Pages

Pages are associated with a route based on their file name. For example, in development:

  • pages/index.js is associated with the / route.

  • pages/posts/first-post.js is associated with the /posts/first-post route.

  • pages/event/[id].js for dynamic routes(see below)

  • pages/404.js for custom 404 and [other errors](Error Pagesarrow-up-right)

import Link from 'next/link'; //must have a tag inside

export default function FirstPost() {
  return (
    <>
      <h1>First Post</h1>
      <h2>
        <Link
          href={{
            pathname: '/about',
            query: { name: 'test' },
          }}
        >
          About us
        </Link>
        <Link href={`/blog/${encodeURIComponent(post.slug)}`}>
           {post.title}
        </Link>
        <Link
          href={{
            pathname: '/about',
            query: { name: 'test' },
          }}
        >
          About us
        </Link>
      </h2>
    </>
  )
}

Yes that is all you need no imports, it somehow auto imports React and such

Next.js automatically prefetches the code for the linked page in the background.

Outer Component

Next.js specific top-level App component

Can keep state when navigating between pages, add global styles, etc

/pages/_app.js

Must restart dev server after adding _app.js

Layouts

Use regular react components

Routes like /post/[id]

  1. Create a [] in the filename to indicate dynamic pages in Next.js

  2. Create file with getStaticPaths arrow-up-rightand getStaticProps

pages/posts/[id].js

Linking

Catch-all Routes

pages/posts/[...id].js matches /posts/a, but also /posts/a/b, /posts/a/b/c and so on.

Router

If you want to access the Next.js router, you can do so by importing the useRouter hook from next/router. Take a look at our router documentationarrow-up-right to learn more.

In next.config.js

Redirecting in JS

Last updated