Any route like `/post/1`, `/post/abc`, etc will be matched by `pages/post/[pid].js`.
Any route like `/post/1`, `/post/abc`, etc will be matched by `pages/post/[pid].js`.
The matched path parameter will be sent as a query parameter to the page.
The matched path parameter will be sent as a query parameter to the page.
> Note: A `<Link>` for a Dynamic Route looks like so:
For example, the route `/post/abc` will have the following `query` object: `{ pid: 'abc' }`.
>
> ```jsx
> <Link href="/post/[pid]" as="/post/abc">
> <a>First Post</a>
> </Link>
> ```
>
> You can [read more about `<Link>` here](#with-link).
For example, the route `/post/1` will have the following `query` object: `{ pid: '1' }`.
Similarly, the route `/post/abc?foo=bar` will have the `query` object: `{ foo: 'bar', pid: 'abc' }`.
Similarly, the route `/post/abc?foo=bar` will have the `query` object: `{ foo: 'bar', pid: 'abc' }`.
A `<Link>` for `/post/abc` looks like so:
```jsx
<Linkhref="/post/[pid]"as="/post/abc">
<a>First Post</a>
</Link>
```
-`href`: the path inside `pages` directory
-`as`: the path that will be rendered in the browser URL bar
> You can [read more about `<Link>` here](#with-link).
However, if a query and route param name are the same, route parameters will override the matching query params.
However, if a query and route param name are the same, route parameters will override the matching query params.
For example, `/post/abc?pid=bcd` will have the `query` object: `{ pid: 'abc' }`.
For example, `/post/abc?pid=bcd` will have the `query` object: `{ pid: 'abc' }`.
...
@@ -404,7 +407,7 @@ When you need state, lifecycle hooks or **initial data population** you can expo
...
@@ -404,7 +407,7 @@ When you need state, lifecycle hooks or **initial data population** you can expo
Using a stateless function:
Using a stateless function:
```jsx
```jsx
importfetchfrom'isomorphic-unfetch';
importfetchfrom'isomorphic-unfetch'
functionPage({stars}){
functionPage({stars}){
return<div>Next stars: {stars}</div>
return<div>Next stars: {stars}</div>
...
@@ -528,36 +531,6 @@ function About() {
...
@@ -528,36 +531,6 @@ function About() {
exportdefaultAbout
exportdefaultAbout
```
```
**Dynamic Routes**
`<Link>` component has two relevant props when using _Dynamic Routes_:
-`href`: the path inside `pages` directory
-`as`: the path that will be rendered in the browser URL bar
Consider the page `pages/post/[postId].js`:
```jsx
import{useRouter}from'next/router'
constPost=()=>{
constrouter=useRouter()
const{postId}=router.query
return<p>My Blog Post: {postId}</p>
}
exportdefaultPost
```
A link for `/post/first-post` looks like this:
```jsx
<Linkhref="/post/[postId]"as="/post/first-post">
<a>First Post</a>
</Link>
```
**Custom routes (using props from URL)**
**Custom routes (using props from URL)**
If you find that your use case is not covered by [Dynamic Routing](#dynamic-routing) then you can create a custom server and manually add dynamic routes.
If you find that your use case is not covered by [Dynamic Routing](#dynamic-routing) then you can create a custom server and manually add dynamic routes.