A dynamic route in Next.js is a page route that uses square brackets [param] in the filename to capture variable parts of the URL, allowing the page to render different content based on the URL segment.
Click to reveal answer
beginner
How do you define a dynamic route for a user profile page with a user ID in Next.js?
You create a file named [userId].js inside the pages or app directory. The [userId] part captures the user ID from the URL, like /profile/123, where 123 is the userId.
Click to reveal answer
intermediate
How do you access the dynamic parameter inside a Next.js page component?
You use the useRouter hook from 'next/router' or the params object in server components to get the dynamic parameter value from the URL.
Click to reveal answer
beginner
What is the difference between a static route and a dynamic route in Next.js?
A static route has a fixed path like /about, while a dynamic route uses [param] to match many paths like /product/1, /product/2, where the param changes.
Click to reveal answer
intermediate
Can you have multiple dynamic segments in a Next.js route? How?
Yes, you can have multiple dynamic segments by naming files like [category]/[id].js. This captures two parameters from the URL, for example /books/123.
Click to reveal answer
What does the file name [postId].js represent in Next.js routing?
AA dynamic route capturing postId from the URL
BA static route named postId
CA component that cannot be routed
DA special API route
✗ Incorrect
Files with [param] in their name define dynamic routes that capture the param value from the URL.
How do you get the value of a dynamic route parameter inside a client component?
AUsing window.location.pathname
BUsing getStaticProps only
CUsing useRouter hook from 'next/router'
DUsing CSS selectors
✗ Incorrect
The useRouter hook provides access to the dynamic route parameters in client components.
Which URL matches the dynamic route file [category]/[id].js?
A/category
B/books/123
C/123/books
D/books
✗ Incorrect
The URL /books/123 matches the dynamic segments category=books and id=123.
What happens if you visit a dynamic route URL that does not match any file?
ANext.js shows a 404 page
BNext.js redirects to home
CNext.js crashes
DNext.js shows a blank page
✗ Incorrect
If no matching route file exists, Next.js shows the 404 not found page.
Can dynamic routes be nested inside folders in Next.js?
AOnly static routes can be nested
BNo, all dynamic routes must be in root
CDynamic routes must be flat files
DYes, you can nest dynamic routes in folders
✗ Incorrect
Dynamic routes can be nested inside folders to create complex URL structures.
Explain how to create and use a dynamic route with [param] in Next.js.
Think about how the file name relates to the URL and how to get the value inside the component.
You got /4 concepts.
Describe the difference between static and dynamic routes in Next.js and when to use each.
Consider how URLs look and what content changes.
You got /4 concepts.
Practice
(1/5)
1. What does the file name [id].js inside the pages folder in Next.js represent?
easy
A. A static page named literally 'id'
B. A dynamic route where id is a variable part of the URL
C. A special API route for handling requests
D. A CSS module file for styling
Solution
Step 1: Understand Next.js dynamic route naming
Files with names in square brackets like [id].js represent dynamic routes where the part inside brackets is a variable.
Step 2: Interpret the meaning of [id].js
This means the URL can have any value in place of id, and the page will render accordingly.
Final Answer:
A dynamic route where id is a variable part of the URL -> Option B
Quick Check:
Dynamic route = [param] file name [OK]
Hint: Square brackets mean dynamic URL part [OK]
Common Mistakes:
Thinking it's a static page named 'id'
Confusing with API routes
Assuming it's a styling file
2. Which of the following is the correct way to access the dynamic parameter slug in a Next.js page component inside pages/[slug].js?
easy
A. const { slug } = props.params;
B. const slug = params.slug;
C. const slug = useRouter().query.slug;
D. const slug = window.location.slug;
Solution
Step 1: Recall how to get dynamic params in Next.js client-side
Next.js provides the useRouter hook to access route parameters via router.query.
Step 2: Identify correct syntax
The correct way is const slug = useRouter().query.slug; to get the dynamic slug value.
Hint: Use useRouter().query.param for dynamic params [OK]
Common Mistakes:
Trying to destructure from props.params directly
Using window.location which doesn't have slug
Using undefined variable params
3. Given the file 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?
medium
A. Post ID: postId
B. Error: router is undefined
C. Post ID: undefined
D. Post ID: 42
Solution
Step 1: Understand dynamic route matching
The file [postId].js matches URLs like /blog/42 and sets postId to '42'.
Step 2: Check how router.query.postId is used
The component uses router.query.postId to display the dynamic value, so it will show '42'.
Final Answer:
Post ID: 42 -> Option D
Quick Check:
Dynamic param value shown = 42 [OK]
Hint: router.query.param shows URL dynamic value [OK]
Common Mistakes:
Expecting literal 'postId' string output
Assuming undefined because of initial render
Thinking router is not defined
4. You have a dynamic route file pages/product/[productId].js. The component tries to access params.productId directly but gets an error. What is the likely cause?
medium
A. The component should use useRouter().query.productId instead of params.productId
B. The file name should be productId.js without brackets
C. Dynamic routes do not support parameters
D. You must import params from 'next/params'
Solution
Step 1: Identify how to access dynamic params in client components
In Next.js, dynamic route params are accessed via useRouter().query in client components, not directly from params.
Using params.productId without proper context or props causes undefined errors.
Final Answer:
The component should use useRouter().query.productId instead of params.productId -> Option A
Quick Check:
Use useRouter().query for dynamic params [OK]
Hint: Use useRouter().query, not params directly [OK]
Common Mistakes:
Trying to access params without props or hooks
Renaming file incorrectly
Expecting automatic import of params
5. You want to create a dynamic route in Next.js that matches URLs like /user/123/profile. Which folder and file structure inside app directory correctly implements this using dynamic segments?