Link & Navigation
Next.js · Reference cheat sheet
Link & Navigation
Next.js · Reference cheat sheet
📋 Overview
Use next/link for client-side soft navigation and next/navigation hooks for programmatic routing in the App Router. Prefer Link over raw <a> for internal routes.
🔧 Core concepts
| API | Router | Purpose |
|---|---|---|
Link | Both | Prefetch + soft navigate |
useRouter (next/navigation) | App | push, replace, refresh, back |
usePathname / useSearchParams | App | Read location |
useRouter (next/router) | Pages | Legacy router |
redirect / notFound | App (Server) | Server-side navigation / 404 |
Link prop | Notes |
|---|---|
href | String or URL object |
prefetch | Default true in many cases; disable for rare routes |
replace | Replace history entry |
scroll | Scroll to top (default true) |
💡 Examples
Basic Link:
import Link from "next/link";
export function Nav() {
return <Link href="/docs">Docs</Link>;
}Programmatic navigation (Client):
"use client";
import { useRouter } from "next/navigation";
export function Go() {
const router = useRouter();
return <button onClick={() => router.push("/dashboard")}>Open</button>;
}Server redirect:
import { redirect } from "next/navigation";
export default function Page() {
redirect("/login");
}⚠️ Pitfalls
useRouterfromnext/routervsnext/navigation— wrong import breaks App Router pages.useSearchParams()can force a client Suspense boundary — wrap accordingly.- External URLs should use
<a>(orLinkwith absolute URL); don't treat them like soft navigations for SEO-critical exit links without care.