rsnext/docs/routing/introduction.md

116 lines
3.1 KiB
Markdown
Raw Normal View History

---
description: Next.js has a built-in, opinionated, and file-system based Router. You can learn how it works here.
---
New docs (#9301) * Added the docs from Notion * Updated the links from notion to relative links * Added a routes manifest to the docs * Removed the <br> after examples toggle * Use the name of the section instead of Introduction * Fixed spelling errors * Optimize the content for Algolia * Add a paragraph for `pageProps` * Add welcome section * Transpile -> Compile * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Test extra room between * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update getting-started.md * Update manifest.json * Update getting-started.md * Update getting-started.md * Add concepts section * Update pages.md * Update pages.md * Add data fetching section * Update pages.md * See how a card looks like * Undo card changes * Added related section to getting-started * Fixed wrong markdown syntax in the withRouter page * Moved the server-side-and-client-side section * Updated next-cli reference * updated getInitialProps section * Minor fixes * Added more Related sections * Add html to the related section in getting-started * Use small for the card * Use cards for all related sections * Added src directory docs * Added src directory to the manifest * Add note about API routes in next export * Add initial data fetching docs (private until new methods are released) * Fix typos * Improve wording * Update getting-started.md * Update getting-started.md * Move advanced concepts to advanced section * Hide server-side vs client-side * Move AMP support * Move typescript into one page * Add routing concepts page * Remove introduction page * Update section on different route types * Update routing.md * Update routing.md * Update routing.md * Update routing.md * Combine router injection pages * Update pages.md * Update routing.md * Update using-link.md * Update using-link.md * Update typescript.md * Move the API Routes typescript to basic features * Added links to the typescript section * Updated links to useRouter and withRouter * Add singleLevel prop to manifest * Added single page for router docs * Updated description * Updated the routes in the manifest * Add data fetching section * Update data-fetching.md * Update data-fetching.md * Update dynamic-routes.md * Update manifest.json * Only use the single router API page * Moved the concepts pages * Updated links * Removed extra space * Updated title for Router API * Added a description with frontmatter * Add open prop to the manifest * Added datafetching section to API Reference * Updated links to the getInitialProps reference * Moved some sections to API * Added next/head to API reference * Added next/link to the API Reference * Removed the populating-head section * Updated links to the new next/link API * Added link from dynamic-routes to next/link docs * use a paragraph * Added next/router API * Added next/amp * Updated the docs for next/amp * Moved the AMP support folder * Updated title * Content updates * Added more links to the data fetching section * Added links from the API to introductions * changing the router API * Updates to the router API * Updated the routing section * life improvements * Added shallow routing section * Small fix * Removed old routing sections * Updated link to shallow routing * Removed unrequired page * Removed /pages * Update data-fetching.md * Add initial deployments section * Update manifest.json * Update introduction.md * Update deployment doc * Add static export section updates * link ssg/ssr * Update deployment.md * Add syntax highlighting Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2019-12-23 16:07:38 +01:00
# Routing
Next.js has a file-system based router built on the [concept of pages](/docs/basic-features/pages.md).
When a file is added to the `pages` directory it's automatically available as a route.
The files inside the `pages` directory can be used to define most common patterns.
#### Index routes
The router will automatically route files named `index` to the root of the directory.
- `pages/index.js``/`
- `pages/blog/index.js``/blog`
#### Nested routes
The router supports nested files. If you create a nested folder structure files will be automatically routed in the same way still.
- `pages/blog/first-post.js``/blog/first-post`
- `pages/dashboard/settings/username.js``/dashboard/settings/username`
#### Dynamic route segments
To match a dynamic segment you can use the bracket syntax. This allows you to match named parameters.
- `pages/blog/[slug].js``/blog/:slug` (`/blog/hello-world`)
- `pages/[username]/settings.js``/:username/settings` (`/foo/settings`)
## Linking between pages
The Next.js router allows you to do client-side route transitions between pages, similarly to a single-page application.
A special React component called `Link` is provided to do this client-side route transition.
```jsx
import Link from 'next/link'
function Home() {
return (
<ul>
<li>
<Link href="/">
<a>Home</a>
</Link>
</li>
<li>
<Link href="/about">
<a>About Us</a>
</Link>
</li>
</ul>
)
}
export default Home
```
When linking to a route with [dynamic path segments](/docs/routing/dynamic-routes.md) you have to provide `href` and `as` to make sure the router knows which JavaScript file to load.
- `href` - The name of the page in the `pages` directory. For example `/blog/[slug]`.
- `as` - The url that will be shown in the browser. For example `/blog/hello-world`.
```jsx
import Link from 'next/link'
function Home() {
return (
<ul>
<li>
<Link href="/blog/[slug]" as="/blog/hello-world">
<a>To Hello World Blog post</a>
</Link>
</li>
</ul>
)
}
export default Home
```
## Injecting the router
<details>
<summary><b>Examples</b></summary>
<ul>
<li><a href="https://github.com/zeit/next.js/tree/canary/examples/dynamic-routing">Dynamic Routing</a></li>
</ul>
</details>
To access the [`router` object](/docs/api-reference/next/router.md#router-object) in a React component you can use [`useRouter`](/docs/api-reference/next/router.md#useRouter) or [`withRouter`](/docs/api-reference/next/router.md#withRouter).
In general we recommend using [`useRouter`](/docs/api-reference/next/router.md#useRouter).
## Learn more
The router is divided in multiple parts:
<div class="card">
<a href="/docs/api-reference/next/link.md">
<b>next/link:</b>
<small>Handle client-side navigations.</small>
</a>
</div>
<div class="card">
<a href="/docs/api-reference/next/router.md">
<b>next/router:</b>
<small>Leverage the router API in your pages.</small>
</a>
</div>