Dynamic route parameters let your app show different pages based on parts of the URL. This helps create pages for many items without making a file for each one.
Dynamic route parameters in Remix
app/routes/$paramName.tsx
The $paramName part in the filename tells Remix this is a dynamic segment.
You can access the parameter inside your component using the useParams() hook.
/123 will show User ID: 123.app/routes/$userId.tsx import { useParams } from '@remix-run/react'; export default function UserProfile() { const params = useParams(); return <div>User ID: {params.userId}</div>; }
/blog/my-first-post will show Blog post: my-first-post.app/routes/blog/$slug.tsx import { useParams } from '@remix-run/react'; export default function BlogPost() { const { slug } = useParams(); return <h1>Blog post: {slug}</h1>; }
This component shows product details based on the product ID from the URL. For example, visiting /42 will display the product ID 42.
app/routes/$productId.tsx import { useParams } from '@remix-run/react'; export default function ProductPage() { const { productId } = useParams(); return ( <main> <h1>Product Details</h1> <p>Product ID: {productId}</p> </main> ); }
Dynamic route files must start with $ to tell Remix they are dynamic.
You can have multiple dynamic parameters by nesting folders or using multiple $param files.
Remember to use useParams() inside your component to get the current URL values.
Dynamic route parameters let you create flexible pages that change based on the URL.
Use $paramName in filenames and useParams() in components to access them.
This helps build apps with many similar pages without making many files.
Practice
Solution
Step 1: Understand dynamic routes
Dynamic routes let pages show different content depending on URL parts.Step 2: Identify the purpose
This means one page file can handle many URLs by reading parameters.Final Answer:
Create pages that change content based on the URL -> Option AQuick Check:
Dynamic routes = flexible pages [OK]
- Thinking dynamic routes speed up loading
- Confusing dynamic routes with styling
- Believing dynamic routes block access
Solution
Step 1: Recall Remix dynamic route syntax
Dynamic parameters use a $ prefix in the filename, like$paramName.jsx.Step 2: Match correct filename
For user ID,$userId.jsxis correct; others have extra dots or misplaced $.Final Answer:
$userId.jsx -> Option CQuick Check:
Dynamic param filename = $paramName.jsx [OK]
- Putting $ after the dot in filename
- Using multiple $ signs
- Naming file without $ for dynamic param
routes/$postId.jsx with code:import { useParams } from '@remix-run/react';
export default function Post() {
const params = useParams();
return <div>Post ID: {params.postId}</div>;
}What will be rendered when visiting URL
/123?Solution
Step 1: Understand useParams behavior
useParams() returns an object with keys matching dynamic route names and values from URL.Step 2: Match URL to param
URL /123 sets postId to '123', so params.postId is '123'.Final Answer:
Post ID: 123 -> Option BQuick Check:
useParams() returns URL param values [OK]
- Expecting param name as string instead of value
- Forgetting to import useParams
- Assuming useParams returns undefined
routes/$user.jsx? It contains:import { useParams } from '@remix-run/react';
export default function User() {
const { userId } = useParams();
return <div>User: {userId}</div>;
}Solution
Step 1: Check filename parameter name
Filename uses$user.jsx, so param name is 'user'.Step 2: Check useParams destructuring
Code destructures 'userId' which does not exist in params; should use 'user'.Final Answer:
The filename parameter and useParams key do not match -> Option AQuick Check:
Param name in filename = key in useParams [OK]
- Destructuring wrong param name
- Thinking useParams can't be destructured
- Believing $ in filename is invalid
productId and also fetches product data server-side. Which filename and code snippet correctly use dynamic route parameters and server loader?Solution
Step 1: Check filename for dynamic param
Filename$productId.jsxcorrectly defines dynamic param 'productId'.Step 2: Verify loader and component
Loader usesparams.productIdto fetch product data, matching filename param. Component usesuseLoaderData()and renders correctly.Final Answer:
routes/$productId.jsx with loader using params.productId -> Option DQuick Check:
Filename param = params key = loader fetch param [OK]
- Mismatching param names in filename and loader
- Not using params in loader
- Ignoring useLoaderData for fetched data
Ready to prove it?
Pass the test and this pattern moves unknown
