Complete the code to define a catch-all route file in Next.js.
export default function Page({ params }) {
return <div>Params: {params.slug[1]</div>;
}
export async function generateStaticParams() {
return [{ slug: ['example'] }];
}In catch-all routes, the params.slug is an array. Using .join('/') converts it to a path string.
Complete the code to define a catch-all route folder name in Next.js.
Create a folder named [1] inside the app directory to catch all routes under /docs.To catch all routes under /docs, the folder name must be [...docs] using square brackets and three dots.
Fix the error in the catch-all route component to access the params correctly.
export default function Page({ params }) {
const slug = params.[1];
return <div>Slug length: {slug.length}</div>;
}The catch-all param name is usually slug or the name used in the folder. Access it as params.slug.
Fill both blanks to create a catch-all route that renders the joined path segments and generates static params.
export default function Page({ params }) {
return <h1>Path: {params.[1].[2]('/')}</h1>;
}
export async function generateStaticParams() {
return [
{ slug: ['a', 'b'] },
{ slug: ['x', 'y', 'z'] }
];
}The catch-all param is slug. To display the path, join the array with '/'.
Fill all three blanks to create a catch-all route that renders the path, uses a dynamic segment, and generates static params.
export default function Page({ params }) {
return <div>Full path: {params.[1].[2]('/')}</div>;
}
export async function generateStaticParams() {
return [
{ [3]: ['2024', '06', '15'] }
];
}The catch-all param is slug. Join the array with '/'. The static params key must match the param name slug.