Dynamic route segments let you create pages that change based on the URL. This helps show different content without making many separate pages.
Dynamic route segments in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
app/[segment]/page.js
Use square brackets [] around the folder or file name to mark it as dynamic.
The name inside brackets becomes a variable you can use in your code.
userId changes based on the URL, like /123 or /alice.app/[userId]/page.js
slug is the post identifier in the URL.app/blog/[slug]/page.js
productId segment.app/products/[productId]/page.js
This component shows a user profile page. It reads the dynamic userId from the URL and displays it on the page.
For example, visiting /123 will show "User ID: 123".
'use client'; import { useParams } from 'next/navigation'; export default function UserPage() { const params = useParams(); return ( <main> <h1>User Profile</h1> <p>User ID: {params.userId}</p> </main> ); } // File path: app/[userId]/page.js
Dynamic segments must be inside the app folder in Next.js 13+ App Router.
You can access the dynamic segment value using the useParams() hook.
Dynamic routes help keep your app organized and avoid creating many similar files.
Dynamic route segments let URLs change parts to show different content.
Use square brackets [] in folder or file names to create dynamic routes.
Access the dynamic part in your component with useParams().
Practice
[id].js inside the pages folder represent?Solution
Step 1: Understand Next.js routing conventions
Files inside thepagesfolder define routes. Square brackets[]indicate dynamic segments.Step 2: Interpret
The file[id].jsmeaning[id].jsmatches any URL segment in that position and passes it as a parameter namedid.Final Answer:
A dynamic route segment that matches any value in the URL at that position -> Option DQuick Check:
Dynamic route = [segment] [OK]
- Thinking [id].js is a static page
- Confusing dynamic routes with API routes
- Assuming it matches only the literal 'id'
Solution
Step 1: Recall Next.js dynamic route syntax
Dynamic segments use square brackets around the parameter name inside thepagesfolder.Step 2: Match syntax for username parameter
The correct syntax is[username].jsto capture the username dynamically.Final Answer:
pages/[username].js -> Option CQuick Check:
Dynamic segment uses [param] syntax [OK]
- Using colon or curly braces instead of square brackets
- Naming the file without brackets for dynamic routes
- Confusing dynamic routes with static filenames
pages/blog/[slug].jsand the URL
/blog/hello-world, what will be the value of the slug parameter inside the page component?Solution
Step 1: Identify dynamic segment from file name
The file[slug].jscaptures the URL segment after/blog/asslug.Step 2: Match URL segment to parameter
For URL/blog/hello-world, the segmenthello-worldis assigned toslug.Final Answer:
"hello-world" -> Option AQuick Check:
URL segment after folder = slug value [OK]
- Using folder name as parameter value
- Assuming parameter is the literal filename
- Expecting undefined if parameter not explicitly passed
pages/product/[id].js. Which of the following code snippets correctly accesses the id parameter inside the component?Solution
Step 1: Recall how to access route params in Next.js pages
Next.js uses theuseRouterhook fromnext/routerto access query parameters.Step 2: Identify correct syntax for extracting
The correct way isidconst router = useRouter(); const { id } = router.query;.Final Answer:
const router = useRouter(); const { id } = router.query; -> Option AQuick Check:
useRouter().query gives dynamic params [OK]
- Using props.params which is not standard in Next.js pages
- Using useParams() which is from React Router, not Next.js
- Calling undefined functions like getIdFromUrl()
/dashboard/user/123 where 123 is a user ID. Which file structure correctly implements this?Solution
Step 1: Analyze the URL structure
The URL/dashboard/user/123has three segments:dashboard,user, and a dynamicid.Step 2: Match file structure to URL segments
Static segmentsdashboardanduserare folders, and[id].jscaptures the dynamic user ID.Step 3: Verify options
pages/dashboard/user/[id].js matches the folder structure exactly. Options B and D incorrectly treat static parts as dynamic. pages/dashboard/[id].js misses theuserfolder.Final Answer:
pages/dashboard/user/[id].js -> Option BQuick Check:
Static folders + [id].js for dynamic segment [OK]
- Making static parts dynamic segments
- Omitting intermediate folders for static URL parts
- Confusing order of folders and dynamic files
