Skip to content

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:

Single dynamic route

When we click each link, the URL changes in the browser’s address bar, but the content in the window remains the same.

Why?

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:

Terminal window
http://localhost:3000/news/business

…the $category value would be the string β€œbusiness”.

However, if we visited

Terminal window
http://localhost:3000/news/reviews

…the $category value would then be the string β€œreviews”, and so on.

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:

app/routes/news.$category.tsx
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:

Logging route params

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.

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 the category value from the URL.
  • Logged the category value to the console to see what it looks like.

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.