Dynamic routes let your app show different pages based on the URL. This helps create pages for many items without making each page by hand.
Dynamic routes with [param] in NextJS
app/[param]/page.jsx
// or in pages directory
pages/[param].jsxThe folder or file name uses square brackets [param] to mark a dynamic part.
The param name is the variable you use to get the value from the URL.
id is the URL part you want to read.app/[id]/page.jsx export default function Page({ params }) { return <p>ID is {params.id}</p>; }
slug is the dynamic part of the URL for a blog post.pages/[slug].jsx export default function Post({ params }) { return <h1>Post: {params.slug}</h1>; }
This component shows a product page. The productId comes from the URL, so if you visit /product/123, it shows "Product ID: 123".
app/product/[productId]/page.jsx export default function ProductPage({ params }) { return ( <main> <h1>Product Details</h1> <p>Product ID: {params.productId}</p> </main> ); }
Dynamic route parameters are always strings from the URL.
Use the params object in your component to access the dynamic parts.
Dynamic routes help keep your app organized and scalable.
Dynamic routes use square brackets [param] in file or folder names.
The params object gives you the dynamic value from the URL.
This lets you create many pages with one component by changing content based on the URL.
Practice
[id].js inside the pages folder in Next.js represent?Solution
Step 1: Understand Next.js dynamic route naming
Files with names in square brackets like[id].jsrepresent dynamic routes where the part inside brackets is a variable.Step 2: Interpret the meaning of
This means the URL can have any value in place of[id].jsid, and the page will render accordingly.Final Answer:
A dynamic route whereidis a variable part of the URL -> Option BQuick Check:
Dynamic route =[param]file name [OK]
- Thinking it's a static page named 'id'
- Confusing with API routes
- Assuming it's a styling file
slug in a Next.js page component inside pages/[slug].js?Solution
Step 1: Recall how to get dynamic params in Next.js client-side
Next.js provides theuseRouterhook to access route parameters viarouter.query.Step 2: Identify correct syntax
The correct way isconst slug = useRouter().query.slug;to get the dynamicslugvalue.Final Answer:
const slug = useRouter().query.slug; -> Option CQuick Check:
useRouter().query.param gets dynamic route param [OK]
- Trying to destructure from props.params directly
- Using window.location which doesn't have slug
- Using undefined variable params
pages/blog/[postId].js with this code:import { useRouter } from 'next/router';
export default function Post() {
const router = useRouter();
return <div>Post ID: {router.query.postId}</div>;
}What will be rendered when the user visits
/blog/42?Solution
Step 1: Understand dynamic route matching
The file[postId].jsmatches URLs like/blog/42and setspostIdto '42'.Step 2: Check how router.query.postId is used
The component usesrouter.query.postIdto display the dynamic value, so it will show '42'.Final Answer:
Post ID: 42 -> Option DQuick Check:
Dynamic param value shown = 42 [OK]
- Expecting literal 'postId' string output
- Assuming undefined because of initial render
- Thinking router is not defined
pages/product/[productId].js. The component tries to access params.productId directly but gets an error. What is the likely cause?Solution
Step 1: Identify how to access dynamic params in client components
In Next.js, dynamic route params are accessed viauseRouter().queryin client components, not directly fromparams.Step 2: Understand why
Usingparams.productIdcauses errorparams.productIdwithout proper context or props causes undefined errors.Final Answer:
The component should useuseRouter().query.productIdinstead ofparams.productId-> Option AQuick Check:
Use useRouter().query for dynamic params [OK]
- Trying to access params without props or hooks
- Renaming file incorrectly
- Expecting automatic import of params
/user/123/profile. Which folder and file structure inside app directory correctly implements this using dynamic segments?Solution
Step 1: Understand Next.js app router folder structure
Each folder represents a URL segment. Dynamic segments use square brackets like[id].Step 2: Map URL
The URL has three parts: 'user', dynamic '123', and 'profile'. So folders should be/user/123/profileto foldersuser/[id]/profilewith apage.jsinside.Final Answer:
app/user/[id]/profile/page.js-> Option AQuick Check:
Dynamic folder inside URL path =[param]folder [OK]
- Placing dynamic param after static folder incorrectly
- Using dash instead of folder for dynamic segment
- Putting dynamic param in wrong folder level
Ready to prove it?
Pass the test and this pattern moves unknown
