Remind yourself how we created a dynamic page for the different news categories.
If you remember, we made the route dynamic by using the $category
route param that is defined in the filename itself:
In a similar way, we need to create another dynamic route, but this time for single articles. The article will be found by its id
field.
With this knowledge, can you create a new dynamic route for the single article page?
The new page should be accessible at the URL /article/$articleId
in the broswer.
The articleId
doesnโt need to be a real id
from the database at the moment - remember that route params are dynamic and can be anything.
For the time being, just display a fake page title and the articleId
in the page content.
So, for example, if you visit http://localhost:3000/article/my-fake-article-id , you should see a page with:
the title โMy amazing news articleโ
the text โArticle ID: my-fake-article-idโ
After youโve had a go at this yourself, check out the solution below.
Create a new file in the src/routes
directory called article.$articleId.tsx
:
1. Create a new route file
To display a fake article and the articleId
in the page content, add the following code to the article.$articleId.tsx
file:
import { type LoaderFunctionArgs , json } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
export async function loader ({ params }: LoaderFunctionArgs ) {
const { articleId } = params
return json ({ articleId })
export default function ArticlePage () {
const { articleId } = useLoaderData < typeof loader >()
< div className = "container py-16" >
< h2 className = "pb-8 text-h2" > My amazing news article </ h2 >
< p > Article ID: { articleId } </ p >
With this in place, you should see the fake article title and the articleId
in the page content when you navigate to /article/$articleId
:
In this guide, we have:
Created a new dynamic page for the single article page.
Displayed a fake article title and the articleId
in the page content.
Verified that the dynamic route is working.