Commit c8bd4271 authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by Joe Haddad

Dynamic routing documentation (#7602)

* Added basic example for dynamic routing

* Added some basic docs for dynamic routing

* Added more docs for dynamic routing

* Used doctoc

* fixed lint issues

* Added useRouter to docs

* Removed getInitialProps examples that are only using router props

* Added useRouter to the navigation

* Small fix

* Updated to the latest dynamic routes implementation

* lint fix

* Update dynamic routing example

* Tweak dynamic routing documentation

* Make basic example basic again

* Adjust dynamic routes section

* Remove query param for dynamic routes example

* Remove old note

* Fix linting
parent def94e61
# Dynamic Routing example
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
```bash
npx create-next-app --example dynamic-routing dynamic-routing-app
# or
yarn create next-app --example dynamic-routing dynamic-routing-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/dynamic-routing
cd dynamic-routing
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [Now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example shows usage of dynamic routing.
This example contains two dynamic pages:
1. `pages/post/[id]/index.js`
- e.g. matches `/post/my-example` (`/post/:id`)
1. `pages/post/[id]/[comment].js`
- e.g. matches `/post/my-example/a-comment` (`/post/:id/:comment`)
These routes are automatically matched by the server.
You can use `next/link` as displayed in this example to route to these pages client side.
import Link from 'next/link'
const Header = () => (
<header>
<ul>
<li>
<Link href='/'>
<a>Home</a>
</Link>
</li>
<li>
<Link href='/about'>
<a>About</a>
</Link>
</li>
<li>
<Link href='/post/[id]' as='/post/first'>
<a>First Post</a>
</Link>
</li>
<li>
<Link href='/post/[id]' as='/post/second'>
<a>Second Post</a>
</Link>
</li>
</ul>
</header>
)
export default Header
{
"version": 2,
"builds": [{ "src": "package.json", "use": "@now/next@canary" }]
}
{
"name": "dynamic-routing",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"license": "ISC",
"dependencies": {
"next": "8.1.1-canary.61",
"react": "16.8.6",
"react-dom": "16.8.6"
}
}
import Header from '../components/header'
const About = () => (
<>
<Header />
<h1>About page</h1>
</>
)
export default About
import Header from '../components/header'
const Home = () => (
<>
<Header />
<h1>Hello World!</h1>
</>
)
export default Home
import { useRouter } from 'next/router'
import Header from '../../../components/header'
const Comment = () => {
const router = useRouter()
const { id, comment } = router.query
return (
<>
<Header />
<h1>Post: {id}</h1>
<h1>Comment: {comment}</h1>
</>
)
}
export default Comment
import { useRouter } from 'next/router'
import Link from 'next/link'
import Header from '../../../components/header'
const Post = () => {
const router = useRouter()
const { id } = router.query
return (
<>
<Header />
<h1>Post: {id}</h1>
<ul>
<li>
<Link href='/post/[id]/[comment]' as={`/post/${id}/first-comment`}>
<a>First comment</a>
</Link>
</li>
<li>
<Link href='/post/[id]/[comment]' as={`/post/${id}/second-comment`}>
<a>Second comment</a>
</Link>
</li>
</ul>
</>
)
}
export default Post
This diff is collapsed.
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