Unverified Commit 242dc7e3 authored by niketchandivade's avatar niketchandivade Committed by GitHub

Examples: Updated the example of catch all routes to utilize the App Router (#68270)

This PR updates the catch-all-routes example to use the App Router. Here
are the changes that have been made:

- Renamed the "pages" folder to the "app" folder.
- Updated the routing for /, /about, and /post/[...slug] files to align
with the App Router.
- Added the layout.tsx file as part of the App Router.
- Renamed used of next/router instead of used params
- Updated the package.json file.
- Updated the Readme file.

CC @samcx

---------
Co-authored-by: 's avatarniket <niket.chandivade@qed42.com>
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent c72fc330
# Catch All Routes Example # Catch All Routes Example
This example shows how to use [Catch all routes](https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes) in Next.js, which allows a dynamic route to catch all paths. This example shows how to use [Catch all routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) in Next.js, which allows a dynamic route to catch all paths.
The catch all page is in `pages/post/[...slug]`, it matches any path after `/post`, like the following: The catch all page is in `app/post/[...slug]/page.tsx`, it matches any path after `/post`, like the following:
- `/post/first-post`, - `/post/first-post`,
- `/post/2020/first-post` - `/post/2020/first-post`
......
import Header from "../components/header";
const About = () => ( const About = () => (
<> <>
<Header />
<h1>About page</h1> <h1>About page</h1>
</> </>
); );
......
import Header from "./_components/header";
export const metadata = {
title: "Next.js",
description: "Generated by Next.js",
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<Header />
{children}
</body>
</html>
);
}
import Header from "../components/header";
const Home = () => ( const Home = () => (
<> <>
<Header />
<h1>Hello World!</h1> <h1>Hello World!</h1>
</> </>
); );
......
import { useRouter } from "next/router"; const Comment = ({ params }: { params: { slug: string[] } }) => {
import Header from "../../components/header"; const slug = params?.slug;
const Comment = () => {
const router = useRouter();
const slug = (router.query.slug as string[]) || [];
return ( return (
<> <>
<Header />
<h1>Slug: {slug.join("/")}</h1> <h1>Slug: {slug.join("/")}</h1>
</> </>
); );
......
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.3.1",
"react-dom": "^18.2.0" "react-dom": "^18.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.0.0", "@types/node": "^22.0.0",
"@types/react": "^18.0.14", "@types/react": "^18.3.3",
"@types/react-dom": "^18.0.5", "@types/react-dom": "^18.3.0",
"typescript": "^4.7.4" "typescript": "^5.5.4"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment