0
0
NextJSframework~20 mins

Dynamic routes with [param] 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 will this dynamic route component render?
Given the Next.js dynamic route file app/products/[id]/page.tsx with this code, what will be rendered when navigating to /products/42?
NextJS
import { useParams } from 'next/navigation';

export default function ProductPage() {
  const params = useParams();
  return <main><h1>Product ID: {params.id}</h1></main>;
}
A<main><h1>Product ID: {id}</h1></main>
B<main><h1>Product ID: </h1></main>
C<main><h1>Product ID: undefined</h1></main>
D<main><h1>Product ID: 42</h1></main>
Attempts:
2 left
💡 Hint
Remember that useParams() returns an object with the dynamic route parameters.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a dynamic route file for a username parameter?
You want to create a dynamic route in Next.js to handle URLs like /profile/john. Which filename is correct to capture the username as a parameter?
Aapp/profile/username/page.tsx
Bapp/profile/[username]/page.tsx
Capp/profile/{username}/page.tsx
Dapp/profile/:username/page.tsx
Attempts:
2 left
💡 Hint
Next.js uses square brackets to define dynamic route segments.
state_output
advanced
2:30remaining
What is the output of this dynamic route component with optional catch-all?
Consider this Next.js dynamic route component in app/blog/[...slug]/page.tsx. What will it render when visiting /blog/2023/06/nextjs?
NextJS
import { useParams } from 'next/navigation';

export default function BlogPage() {
  const { slug } = useParams();
  return <main><h1>Slug: {Array.isArray(slug) ? slug.join(' / ') : slug}</h1></main>;
}
A<main><h1>Slug: 2023 / 06 / nextjs</h1></main>
B<main><h1>Slug: 2023,06,nextjs</h1></main>
C<main><h1>Slug: 2023-06-nextjs</h1></main>
D<main><h1>Slug: undefined</h1></main>
Attempts:
2 left
💡 Hint
Catch-all routes receive an array of path segments in the parameter.
🔧 Debug
advanced
2:30remaining
Why does this dynamic route component throw an error?
This Next.js dynamic route component in app/user/[id]/page.tsx throws an error when visiting /user/123. What is the cause?
NextJS
import { useParams } from 'next/navigation';

export default function UserPage() {
  const id = useParams().id;
  return <main><h1>User ID: {id.toUpperCase()}</h1></main>;
}
Aid is undefined, so calling toUpperCase() causes a TypeError
BuseParams() returns a number, so toUpperCase() is not a function
CThe component is missing a return statement
DDynamic routes cannot use useParams() in Next.js
Attempts:
2 left
💡 Hint
Check if id is always defined and its type before calling string methods.
🧠 Conceptual
expert
3:00remaining
How does Next.js handle dynamic route parameters in server components?
In Next.js App Router, how are dynamic route parameters accessed inside a server component located at app/posts/[postId]/page.tsx?
AYou must use the useParams() hook inside the server component
BYou import the parameters from 'next/params' module
CThey are passed as a prop named 'params' to the server component
DDynamic parameters are not accessible in server components
Attempts:
2 left
💡 Hint
Server components receive route params as props automatically.