Linking and Navigation in Next.js
Client-side routing with next/link: automatic prefetching, soft navigation, useRouter hook, usePathname, useSearchParams, and redirect().
Linking and Navigation in Next.js
Next.js provides client-side, single-page navigation that preserves React state and pre-fetches route bundles in the background.
1. The <Link> Component
Always use next/link instead of native <a href="..."> tags for internal navigation:
import Link from "next/link";
export function Navigation() {
return (
<nav className="flex gap-4">
<Link href="/" className="hover:underline">
Home
</Link>
<Link href="/docs/javascript" prefetch={true} className="hover:underline">
JavaScript Docs
</Link>
</nav>
);
}Why <Link> is Superior:
- Prefetching: When a
<Link>appears in the user's viewport, Next.js automatically pre-fetches the route in the background, making clicks feel instantaneous. - Soft Navigation: Only changed layout and page segments are re-rendered; existing layout state is preserved.
2. Programmatic Navigation with useRouter (Client Components)
"use client";
import { useRouter } from "next/navigation";
export function RedirectButton() {
const router = useRouter();
function handleClick() {
// Navigate programmatically:
router.push("/dashboard");
}
return <button onClick={handleClick}>Go to Dashboard</button>;
}3. Server-Side Redirects (redirect())
In Server Components, Server Actions, or Route Handlers:
import { redirect } from "next/navigation";
export default async function ProtectedPage() {
const session = await getAuthSession();
if (!session) {
redirect("/login"); // Throws internal redirect exception
}
return <div>Welcome to private dashboard</div>;
}Related Topics
Route Groups in Next.js App Router
Organizing routes with Route Groups (folderName): omitting folder names from URLs, creating multiple root layouts, and organizing dashboard vs marketing routes.
React Server Components (RSC) in Next.js
Mastering React Server Components: server-only execution, direct database access, zero client bundle size, and passing data to Client Components.