Linking and moving between pages

Beginner · 5 min

The Link component, prefetching, client-side transitions, useRouter and usePathname, and what makes a Next.js navigation feel instant or slow.

Before thisPages and layouts

Codevolution · 5 min

Linking and moving between pages

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<Link>` extends the HTML anchor with client-side transitions and prefetching: shared layouts stay mounted, and only the changed part of the page is fetched and swapped.
  • Next.js prefetches linked routes as they enter the viewport; a static route is prefetched in full, while a dynamic route is skipped or only partially prefetched, which is why `loading.tsx` matters for dynamic routes.
  • `useRouter` (`push`, `replace`, `back`, `refresh`) is for changing routes from event handlers in Client Components; for ordinary links, prefer `<Link>`, which renders a real anchor.
  • Active-link styling needs the current path, so it takes a small Client Component calling `usePathname`.

A normal <a href="/dashboard"> makes the browser request a new document. Every shared piece of UI is torn down and rebuilt, scroll resets, any client state vanishes, and the user waits through the whole load. In a server-rendered app that wait includes a round trip to your server, and that is the cost Next.js is built to hide.

It uses three techniques together: prefetching, streaming, and client-side transitions. <Link> is the component that wires them up.

import Link from "next/link";

export default function Page() {
  return <Link href="/blog/hello">Read the post</Link>;
}

It renders an ordinary anchor, so right-click, open in new tab and search crawlers all work. Extra props such as className pass through to the <a>. The ones specific to Next.js:

  • replace swaps the current history entry instead of adding one.
  • scroll={false} keeps the scroll position on navigation.
  • prefetch can be set to false to turn off prefetching for this link.
  • href also accepts an object, such as { pathname: "/about", query: { name: "test" } }.

For dynamic URLs, build the string: <Link href={`/products/${id}`}>. Next.js 16 also has useLinkStatus, a hook that tells a component inside a link whether that link’s transition is pending, for showing a spinner on slow networks.

Prefetching

When a <Link> scrolls into view, Next.js loads the target route in the background so that clicking it has nothing left to wait for. How much it loads depends on the route:

  • A static route is prefetched in full.
  • A dynamic route is skipped, or partially prefetched if a loading.tsx exists. “Partially” means the shared layouts and the loading fallback, not the data.

Skipping the data is deliberate: prefetching a page that depends on a request would do server work for a route the user might never open. That is also why loading UI and streaming is the cure for slow dynamic navigation: the fallback is prefetched, so the click responds at once and the content streams in.

Prefetching only starts after <Link> hydrates, since it is a Client Component. On a slow first visit with big JavaScript bundles, the first few clicks may not be prefetched yet. Opting out with prefetch={false} makes sense for long lists of links, such as a table with hundreds of rows, at the price that static routes load only on click.

Moving between pages from code

Sometimes navigation follows an event. In a Client Component, use the router:

"use client";
import { useRouter } from "next/navigation";

export function OrderButton() {
  const router = useRouter();
  return <button onClick={() => router.push("/orders/new")}>Order</button>;
}

router.push adds a history entry, replace swaps one, back and forward walk history, and refresh re-requests the current route from the server. Import from next/navigation, not next/router, which belongs to the Pages Router. In a Server Component, use redirect() instead, covered in redirects and rewrites.

A link that knows whether it matches the current URL needs the path, and the path lives on the client:

"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";

export function NavLink({ href, children }: { href: string; children: React.ReactNode }) {
  const pathname = usePathname();
  return (
    <Link href={href} aria-current={pathname === href ? "page" : undefined}>
      {children}
    </Link>
  );
}

Marking only this small component as a Client Component keeps the rest of your layout on the server. Setting aria-current also tells assistive technology which link is the current page; style it in CSS with [aria-current="page"].

What the video covers

Codevolution’s chapters cover file-based routing, client-side navigation with <Link>, links between pages, creating dynamic links for products, generating links with template literals, and the replace prop. Prefetching is not a chapter, so this lesson carries it. The programmatic navigation video, linked under deeper, covers useRouter and redirect.

Where this fits

Links are the front end of navigation; loading and error files decide what the user sees while it happens. Next, loading UI and streaming.

Check yourself

  1. 01What is the difference between a client-side transition and a full page load?

    A full page load requests a new document, discards all JavaScript state and resets scroll. A client-side transition fetches only the new route's data, keeps shared layouts mounted with their state, and swaps in the new page without reloading.

  2. 02Why might a link to a dynamic route feel slow even though Next.js prefetches?

    A dynamic route cannot be fully prefetched, because its content depends on the request. Without a `loading.tsx` there is nothing to show until the server responds. With one, the fallback is prefetched and appears immediately.

  3. 03When should you reach for `useRouter().push` instead of `<Link>`?

    When navigation is the result of an event, such as after a button click or a timer, rather than a visible link. For anything the user should be able to see, copy or open in a new tab, use `<Link>`, which renders a real `<a href>`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark