$ npx create-remix@latest pokedex-remix
? Initialize a new git repository?
> Yes
? Install dependencies with npm?
> Yes
$ cd ./pokedex-remix
$ npm run dev
npm run dev
Starts the app in dev mode with hot reload
npm run build
Creates a production build of your app (output in build and public/build folders)
npm start
Starts Node.js server in build folder
npm run typecheck
Runs TypeScript compiler to ensure type-safety in build pipeline (not done automatically by Remix compiler)
_index indicates an index route
→ URLs / and /blog
Static route segments
→ e.g. /about and/blog/archive
Use the dot delimiter for nested routes
→ URLs /blog/…
Dynamic route segments (parameters) start with a dollar symbol
→ e.g. /blog/five-great-tips-for-2024
blog.$slug.tsx
How can we redirect to a different route?
E.g. / → /blog
How can we redirect to a different route?
E.g. / → /blog
_index.tsx
Must be a named export called loader.
_index.tsx
→ redirects to blog.tsx
blog.$slug.tsx
Return a Response object…
…or throw it!
app/routes/_index.tsx
app/routes/pokemon._index.tsx
app/routes/pokemon.$pokemonName.tsx
app/api/fetcher.ts
app/components/layout/Layout.tsx
app/root.tsx
Complete solution code can be found on GitHub on the branch 02-data-fetching.
blog.$slug.tsx
Let's start simple: We can use a native HTML form.
blog.$slug.tsx
On the server the form is processed by an action.
blog.$slug.tsx
Form data is read from the request using the standard Request.formData() method.
blog.$slug.tsx
The data can then be processed as desired (validated, stored, etc.).
blog.$slug.tsx
Like the loader, the
action function can return a Response
object.
Typically, a redirect is returned to
implement the
Post/Redirect/Get
pattern.
blog.$slug.tsx
What will happen when the user clicks the Submit button?
→ It works! But it does a full document request: POST /blog/…
blog.$slug.tsx
The Remix Form component brings the expected SPA behavior.
It still has the native form behavior as
long as JS has not loaded yet.
→ Progressive Enhancement
Remix has built-in support for sessions:
Let's work with the most versatile one:
app/sessions.ts
We set up our session handling utilities.
app/sessions.ts
We set up our session handling utilities.
And can use them in server-side code (typically loaders and actions):
What do we want to show in case of validation errors?
The form, with:
Thus, what do we need to do differently in our action?
Thus, what do we need to do differently in our action?
If request is invalid, return a HTTP 400 response with the field values and errors.
In the component, useActionData to access the data returned by the action:
app/components/profile/Profile.tsx
app/components/profile/Profile.tsx
app/sessions.ts
app/routes/profile.tsx
app/routes/profile.tsx
app/routes/profile.tsx
app/components/layout/Layout.tsx
app/root.tsx
Complete solution code can be found on GitHub on the branch 03-forms-actions.
We learned…