Discover how one simple file can handle hundreds of unique URLs effortlessly!
Why Dynamic routes with [param] in NextJS? - Purpose & Use Cases
Imagine building a website where you have hundreds of user profiles, each with a unique URL like /user/john or /user/mary. You try to create a separate page file for each user manually.
Manually creating a file for every user is impossible to maintain. Every time a new user joins, you must add a new file. It's slow, error-prone, and your project quickly becomes cluttered and hard to update.
Dynamic routes with [param] let you create one flexible page that automatically handles all user URLs. You write code once, and Next.js fills in the details based on the URL, making your app scalable and easy to manage.
pages/user/john.js pages/user/mary.js
pages/user/[username].js
This lets you build scalable websites that respond to many different URLs with just one dynamic page, saving time and reducing errors.
A blog site where each post has its own URL like /post/how-to-cook or /post/learn-nextjs, all handled by a single dynamic route page.
Manual page creation for each URL is not practical.
Dynamic routes use [param] to handle many URLs with one page.
This makes your app scalable, cleaner, and easier to maintain.
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
