Skip to content
RoutingChapter 6 / 34Beginner4 min read

Linking & Navigation

Next.js link navigation explained: the Link component, prefetching, useRouter, usePathname and redirect, for fast client-side navigation between pages.

On this page

1. What are linking and navigation?

Navigation has four main tools: Link (the most common), useRouter (navigate from code), usePathname (the current URL) and redirect (navigate on the server).

2. Why does it matter?

A plain a tag reloads the whole page on every click. The layout is rebuilt, state is lost, and the screen flashes white.

A plain a tagNext.js Link
The whole page reloadsOnly the changed part renders
Layout and state resetLayout and state are preserved
The download starts after the clickPrefetched as soon as it enters the viewport
Feels slowInstant, app-like navigation

In Next.js 16, prefetching has become smarter. Incremental prefetching fetches only the part that is not in the cache, the request is cancelled if the link leaves the viewport, and hovering gives it priority.

3. Where is it used?

SituationTool
Navbar, cards, menu linksLink
Going to another page after a form submituseRouter().push or redirect in a Server Action
Highlighting the active linkusePathname
Send the user away if not logged inredirect("/login") on the server
Refresh data without changing the URLrouter.refresh()

4. How does it work?

The journey of a Link click
The Link enters the viewport
Next.js prefetches the necessary part of the route
The user clicks
Shared layouts stay in place
Only the new page segment renders
The URL updates, scroll goes to the top

Link and useRouter work on the client. redirect works on the server (Server Component, Server Action, Route Handler).

5. Code example

tsx
"use client";

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

const links = [
  { href: "/", label: "Home" },
  { href: "/blog", label: "Blog" },
  { href: "/about", label: "About" },
];

export function NavBar() {
  const pathname = usePathname();

  return (
    <nav>
      {links.map((link) => (
        <Link
          key={link.href}
          href={link.href}
          className={pathname === link.href ? "font-bold" : ""}
        >
          {link.label}
        </Link>
      ))}
    </nav>
  );
}
tsx
"use client";

import { useRouter } from "next/navigation";

export function BackToCart() {
  const router = useRouter();
  return <button onClick={() => router.push("/cart")}>Go to cart</button>;
}
tsx
import { redirect } from "next/navigation";
import { cookies } from "next/headers";

export default async function AccountPage() {
  const session = (await cookies()).get("session");
  if (!session) {
    redirect("/login");
  }
  return <h1>Your account</h1>;
}

How does the code work?

PartMeaning
Link hrefClient-side navigation, automatic prefetch
usePathname()The current path, for the active link
"use client"Hooks work only in a Client Component
router.push("/cart")Navigates from code, adds a history entry
router.replaceNavigates but replaces the history entry
await cookies()In Next.js 16 cookies is async too
redirect("/login")Navigates on the server, the code after it does not run

Interview tip

"Which package does useRouter come from?" In the App Router it comes from next/navigation. next/router belongs only to the old Pages Router.

6. Common mistakes

Mistake: Using a plain a tag for internal links

Why? Every click does a full reload and you lose the benefit of prefetching.

Better: Always use Link for links inside the app, and an a tag is fine for external links.

Mistake: Importing useRouter from next/router

Why? next/router belongs to the Pages Router and gives an error in the App Router.

Better: Import useRouter, usePathname and useSearchParams from next/navigation.

Mistake: Calling redirect inside a try catch

Why? redirect throws a special error internally, the catch swallows it and the redirect does not happen.

Better: Call redirect outside the try catch, after the work is done.

7. What to learn before and after

Final quick revision

PointDetail
Main toolLink from next/link
PrefetchAs soon as it enters the viewport, incremental in Next.js 16
Navigating from codeuseRouter from next/navigation
Current URLusePathname
Server navigationredirect
LayoutPreserved across navigation