next/link

Examples

Before moving forward, we recommend you to read Routing Introduction first.

Client-side transitions between routes can be enabled via the Link component exported by next/link.

For an example, consider a pages directory with the following files:

  • pages/index.js
  • pages/about.js
  • pages/blog/[slug].js

We can have a link to each of these pages like so:

  1. import Link from 'next/link'
  2. function Home() {
  3. return (
  4. <ul>
  5. <li>
  6. <Link href="/">
  7. <a>Home</a>
  8. </Link>
  9. </li>
  10. <li>
  11. <Link href="/about">
  12. <a>About Us</a>
  13. </Link>
  14. </li>
  15. <li>
  16. <Link href="/blog/hello-world">
  17. <a>Blog Post</a>
  18. </Link>
  19. </li>
  20. </ul>
  21. )
  22. }
  23. export default Home

Link accepts the following props:

  • href - The path or URL to navigate to. This is the only required prop. It can also be an object, see example here
  • as - Optional decorator for the path that will be shown in the browser URL bar. Before Next.js 9.5.3 this was used for dynamic routes, check our previous docs to see how it worked. Note: when this path differs from the one provided in href the previous href/as behavior is used as shown in the previous docs.
  • passHref - Forces Link to send the href property to its child. Defaults to false
  • prefetch - Prefetch the page in the background. Defaults to true. Any <Link /> that is in the viewport (initially or through scroll) will be preloaded. Prefetch can be disabled by passing prefetch={false}. When prefetch is set to false, prefetching will still occur on hover. Pages using Static Generation will preload JSON files with the data for faster page transitions. Prefetching is only enabled in production.
  • replace - Replace the current history state instead of adding a new url into the stack. Defaults to false
  • scroll - Scroll to the top of the page after a navigation. Defaults to true
  • shallow - Update the path of the current page without rerunning getStaticProps, getServerSideProps or getInitialProps. Defaults to false
  • locale - The active locale is automatically prepended. locale allows for providing a different locale. When false href has to include the locale as the default behavior is disabled.

If the route has dynamic segments

There is nothing to do when linking to a dynamic route, including catch all routes, since Next.js 9.5.3 (for older versions check our previous docs). However, it can become quite common and handy to use interpolation or an URL Object to generate the link.

For example, the dynamic route pages/blog/[slug].js will match the following link:

  1. import Link from 'next/link'
  2. function Posts({ posts }) {
  3. return (
  4. <ul>
  5. {posts.map((post) => (
  6. <li key={post.id}>
  7. <Link href={`/blog/${encodeURIComponent(post.slug)}`}>
  8. <a>{post.title}</a>
  9. </Link>
  10. </li>
  11. ))}
  12. </ul>
  13. )
  14. }
  15. export default Posts

If the child is a custom component that wraps an tag

If the child of Link is a custom component that wraps an <a> tag, you must add passHref to Link. This is necessary if you’re using libraries like styled-components. Without this, the <a> tag will not have the href attribute, which hurts your site’s accessibility and might affect SEO. If you’re using ESLint, there is a built-in rule next/link-passhref to ensure correct usage of passHref.

  1. import Link from 'next/link'
  2. import styled from 'styled-components'
  3. // This creates a custom component that wraps an <a> tag
  4. const RedLink = styled.a`
  5. color: red;
  6. `
  7. function NavLink({ href, name }) {
  8. // Must add passHref to Link
  9. return (
  10. <Link href={href} passHref>
  11. <RedLink>{name}</RedLink>
  12. </Link>
  13. )
  14. }
  15. export default NavLink
  • If you’re using emotion’s JSX pragma feature (@jsx jsx), you must use passHref even if you use an <a> tag directly.
  • The component should support onClick property to trigger navigation correctly

If the child is a functional component

If the child of Link is a functional component, in addition to using passHref, you must wrap the component in React.forwardRef:

  1. import Link from 'next/link'
  2. // `onClick`, `href`, and `ref` need to be passed to the DOM element
  3. // for proper handling
  4. const MyButton = React.forwardRef(({ onClick, href }, ref) => {
  5. return (
  6. <a href={href} onClick={onClick} ref={ref}>
  7. Click Me
  8. </a>
  9. )
  10. })
  11. function Home() {
  12. return (
  13. <Link href="/about" passHref>
  14. <MyButton />
  15. </Link>
  16. )
  17. }
  18. export default Home

With URL Object

Link can also receive a URL object and it will automatically format it to create the URL string. Here’s how to do it:

  1. import Link from 'next/link'
  2. function Home() {
  3. return (
  4. <ul>
  5. <li>
  6. <Link
  7. href={{
  8. pathname: '/about',
  9. query: { name: 'test' },
  10. }}
  11. >
  12. <a>About us</a>
  13. </Link>
  14. </li>
  15. <li>
  16. <Link
  17. href={{
  18. pathname: '/blog/[slug]',
  19. query: { slug: 'my-post' },
  20. }}
  21. >
  22. <a>Blog Post</a>
  23. </Link>
  24. </li>
  25. </ul>
  26. )
  27. }
  28. export default Home

The above example has a link to:

  • A predefined route: /about?name=test
  • A dynamic route: /blog/my-post

You can use every property as defined in the Node.js URL module documentation.

Replace the URL instead of push

The default behavior of the Link component is to push a new URL into the history stack. You can use the replace prop to prevent adding a new entry, as in the following example:

  1. <Link href="/about" replace>
  2. <a>About us</a>
  3. </Link>

Disable scrolling to the top of the page

The default behavior of Link is to scroll to the top of the page. When there is a hash defined it will scroll to the specific id, like a normal <a> tag. To prevent scrolling to the top / hash scroll={false} can be added to Link:

  1. <Link href="/#hashid" scroll={false}>
  2. <a>Disables scrolling to the top</a>
  3. </Link>