Dynamic routes and loader functions in Remix
With the changes we have made in the previous lesson, we have a news category page set up, but the content is always the same:
When we click each link, the URL changes in the browserβs address bar, but the content in the window remains the same.
Why?
Route params
Section titled βRoute paramsβThe $
sign in the file name news/$category.tsx
is actually a special character that tells Remix that this route is dynamic.
It means that the route can match any base URL that starts with /news/
, but then will have a unique value for category
.
So, for example, if we visited the URL:
http://localhost:3000/news/business
β¦the $category
value would be the string βbusiness
β.
However, if we visited
http://localhost:3000/news/reviews
β¦the $category
value would then be the string βreviews
β, and so on.
Reading route params with the loader
function
Section titled βReading route params with the loader functionβTo read the category
value from the URL, we need to add something called a loader
function to our news/$category.tsx
file.
At first, letβs just log the category
value out to the console and see what it looks like.
Carefully add the following code to your news/$category.tsx
file:
import { type LoaderFunctionArgs, json } from '@remix-run/node'
export async function loader({ params }: LoaderFunctionArgs) { const { category } = params
console.log({ category })
return json({})}
export default function NewsCategoryPage() { return ( <div className="container py-16"> <h2 className="text-h2">Generic news category page</h2> </div> )}
Head back to your browser and click the links between the different news categories.
Now check your VS Code terminal. You should see the category
value logged out each time you click a link:
This is great!
We are loading the dynamic category
value directly from the URL and logging it to the console.
In the next step, we will do something more useful with this value. We will pass it to our NewsCategoryPage
component and display it on the page.
Summary
Section titled βSummaryβIn this step, we have:
- Deleted our hard-coded static child routes and replaced them with a single dynamic route.
- Learned about route params and how they can be used to create dynamic routes in Remix.
- Added a
loader
function to our dynamic route to read thecategory
value from the URL. - Logged the
category
value to the console to see what it looks like.
Whatβs next?
Section titled βWhatβs next?βIn the next step, we will pass the category
value to our NewsCategoryPage
component with Remixβs useLoaderData
hook, and display it on the page.