0
0
NextJSframework~20 mins

Dynamic route segments in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is rendered by this dynamic route component?
Consider a Next.js app with a file named app/products/[id]/page.jsx containing this code:

export default function ProductPage({ params }) {
  return <h1>Product ID: {params.id}</h1>
}

What will be the rendered output when visiting /products/42?
NextJS
export default function ProductPage({ params }) {
  return <h1>Product ID: {params.id}</h1>
}
A<h1>Product ID: 42</h1>
B<h1>Product ID: params.id</h1>
C<h1>Product ID: undefined</h1>
DError: params is undefined
Attempts:
2 left
💡 Hint
Remember that Next.js passes dynamic segment values inside the params object.
📝 Syntax
intermediate
1:30remaining
Which code correctly defines a dynamic route segment in Next.js?
You want to create a dynamic route for blog posts by ID. Which file name correctly defines this dynamic segment?
Aapp/blog/[postId]/page.jsx
Bapp/blog/{postId}/page.jsx
Capp/blog/:postId/page.jsx
Dapp/blog/postId/page.jsx
Attempts:
2 left
💡 Hint
Next.js uses square brackets to mark dynamic segments in file names.
state_output
advanced
2:30remaining
What is the value of params in this nested dynamic route?
Given a Next.js app with the file app/users/[userId]/posts/[postId]/page.jsx containing:

export default function PostPage({ params }) {
  return 
{JSON.stringify(params)}
}

What will be the JSON string rendered when visiting /users/7/posts/15?
NextJS
export default function PostPage({ params }) {
  return <pre>{JSON.stringify(params)}</pre>
}
A{}
B{"userId":"7","postId":"15"}
C{"postId":"15"}
D{"userId":"7/posts/15"}
Attempts:
2 left
💡 Hint
Each dynamic segment in the path becomes a key in params with the matched value.
🔧 Debug
advanced
2:30remaining
Why does this dynamic route component cause an error?
This Next.js dynamic route component is in app/profile/[username]/page.jsx and contains:

export default function Profile({ params }) {
  return 

Welcome, {params.user}!

}

Visiting /profile/alice causes an error. Why?
NextJS
export default function Profile({ params }) {
  return <h2>Welcome, {params.user}!</h2>
}
AThe component must use useRouter() hook to access params.
Bparams is undefined because it is not passed to the component.
Cparams.user is undefined because the dynamic segment is named 'username', not 'user'.
DDynamic routes cannot have more than one segment.
Attempts:
2 left
💡 Hint
Check the dynamic segment name in the file path and how it matches params keys.
🧠 Conceptual
expert
3:00remaining
How does Next.js handle optional catch-all dynamic routes?
In Next.js, you create a file app/docs/[...slug]/page.jsx to catch all routes under /docs. What is the value of params.slug when visiting /docs (no extra path)?
A[''] (an array with an empty string)
Bundefined
Cnull
D[] (an empty array)
Attempts:
2 left
💡 Hint
Optional catch-all routes provide an array of segments or an empty array if none.