0
0
NextJSframework~20 mins

Catch-all routes with [...param] in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Catch-All Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing /docs/2024/06 in a catch-all route?
Given a Next.js catch-all route file named app/docs/[...slug]/page.tsx with this code:
export default function Page({ params }) {
  return 

{JSON.stringify(params.slug)}

}

What will be rendered when visiting /docs/2024/06?
NextJS
export default function Page({ params }) {
  return <p>{JSON.stringify(params.slug)}</p>
}
A["2024", "06"]
B"2024/06"
Cundefined
D[]
Attempts:
2 left
💡 Hint
Catch-all routes collect all path segments into an array under the param name.
📝 Syntax
intermediate
1:30remaining
Which file name correctly defines a catch-all route in Next.js?
You want to create a catch-all route that matches any path under /blog/. Which file name is correct?
Aapp/blog/[slug]/page.tsx
Bapp/blog/[...slug].tsx
Capp/blog/[slug...]/page.tsx
Dapp/blog/[...slug]/page.tsx
Attempts:
2 left
💡 Hint
Catch-all routes use three dots inside square brackets before the param name.
state_output
advanced
2:00remaining
What is the value of params.slug for /shop/electronics/phones in a catch-all route?
Consider a Next.js catch-all route app/shop/[...slug]/page.tsx with this code:
export default function Page({ params }) {
  return 

{Array.isArray(params.slug) ? params.slug.length : 'no slug'}

}

What will be rendered when visiting /shop/electronics/phones?
NextJS
export default function Page({ params }) {
  return <p>{Array.isArray(params.slug) ? params.slug.length : 'no slug'}</p>
}
A3
B2
C"no slug"
D1
Attempts:
2 left
💡 Hint
Count how many segments are after /shop/ in the URL.
🔧 Debug
advanced
2:30remaining
Why does params.slug return undefined in a catch-all route?
You created app/products/[[...slug]]/page.tsx with this code:
export default function Page({ params }) {
  return 

{params.slug.join('-')}

}

But when visiting /products (no extra path), it crashes with TypeError: Cannot read properties of undefined (reading 'join'). Why?
NextJS
export default function Page({ params }) {
  return <p>{params.slug.join('-')}</p>
}
ABecause the file name should be [...slug].tsx, not a folder
BBecause visiting /products matches the route but params.slug is undefined when no segments exist
CBecause join is not a function on arrays in Next.js
DBecause params is undefined in catch-all routes
Attempts:
2 left
💡 Hint
Check what happens when no path segments are matched in a catch-all route.
🧠 Conceptual
expert
3:00remaining
How does Next.js differentiate between optional and required catch-all routes?
In Next.js routing, what is the difference between [...param] and [[...param]] in file names?
A<code>[...param]</code> matches only one segment; <code>[[...param]]</code> matches multiple segments
B<code>[...param]</code> is for dynamic routes; <code>[[...param]]</code> is for static routes
C<code>[...param]</code> requires at least one path segment; <code>[[...param]]</code> allows zero or more segments
D<code>[...param]</code> matches zero or more segments; <code>[[...param]]</code> matches exactly one segment
Attempts:
2 left
💡 Hint
Think about whether the route matches when no extra path is given.