JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsnextjsNavigation & Linking
PrevNext
nextjs
beginner
8 min read

Navigation & Linking

link
navigation
nextjs
prefetching
redirect
routing
usePathname
useRouter

Next.js provides three navigation methods — the <Link> component for declarative navigation with automatic prefetching, useRouter() for programmatic navigation in event handlers, and redirect() for server-side redirects in Server Components and actions.

Key Points

1<Link> Component

Extends <a> with automatic prefetching and client-side navigation — links in the viewport are prefetched, clicking updates only changed segments.

2useRouter() Hook

Programmatic navigation in Client Components — push(), replace(), back(), refresh(). Import from next/navigation, not next/router.

3redirect() Function

Server-side redirects in Server Components and actions — throws internally (can't try/catch). 307 temporary, permanentRedirect() for 308.

4Client-Side Transitions

Navigation updates only changed segments while preserving shared layouts and React state — no full page reload.

What You'll Learn

  • Use <Link> for declarative navigation with prefetching control
  • Implement programmatic navigation with useRouter() in Client Components
  • Apply redirect() for server-side redirects in Server Components
  • Use usePathname() and useSearchParams() for reading the current URL

Deep Dive

Next.js provides several navigation mechanisms, each designed for a specific use case. The key differentiator from plain React is automatic prefetching and client-side transitions that preserve state.

The <Link> Component

The primary navigation mechanism — renders an <a> tag with automatic prefetching and client-side navigation:

JSX
import Link from 'next/link';
 
<Link href="/about">About</Link>
<Link href="/blog/hello-world">Blog Post</Link>
<Link href={{ pathname: '/search', query: { q: 'react' } }}>Search</Link>

<Link> extends the HTML <a> tag with:

  • Automatic prefetching: Links in the viewport are prefetched automatically (static routes load the full page; dynamic routes load up to the nearest loading.tsx boundary)
  • Client-side navigation: Clicking doesn't trigger a full page reload — Next.js updates only the changed segments, preserving shared layouts and React state
  • Scroll restoration: Scrolls to the top on navigation, preserves scroll position on back/forward

Control prefetching: <Link href="/heavy" prefetch={false}> disables automatic prefetch for heavy pages.

useRouter() Hook

For programmatic navigation in event handlers (Client Components only):

JavaScript
'use client';
import { useRouter } from 'next/navigation';
 
function LoginButton() {
  const router = useRouter();
  
  async function handleLogin() {
    await signIn();
    router.push('/dashboard');
  }
  
  return <button onClick={handleLogin}>Login</button>;
}

Key methods:

  • router.push('/path') — Navigate to a new route (adds to history)
  • router.replace('/path') — Navigate without adding to history (login → dashboard)
  • router.back() — Go back in history
  • router.forward() — Go forward in history
  • router.refresh() — Re-fetch the current route's Server Component data without a full page reload
  • router.prefetch('/path') — Manually prefetch a route

Important: Import useRouter from next/navigation (App Router), not next/router (Pages Router).

redirect() Function

For server-side redirects in Server Components, Server Actions, and Route Handlers:

JSX
import { redirect } from 'next/navigation';
 
async function Profile() {
  const session = await getSession();
  if (!session) redirect('/login'); // Server-side redirect
  return <Dashboard user={session.user} />;
}

redirect() throws a special error internally — it cannot be wrapped in try/catch. It sends a 307 (temporary redirect) by default; use permanentRedirect() for 308 (permanent).

usePathname() and useSearchParams()

Hooks for reading the current URL in Client Components:

JSX
'use client';
import { usePathname, useSearchParams } from 'next/navigation';
 
function NavLink({ href, children }) {
  const pathname = usePathname();
  const isActive = pathname === href;
  return <Link href={href} className={isActive ? 'active' : ''}>{children}</Link>;
}

useSearchParams() returns URLSearchParams for reading query string values. Wrap components using useSearchParams in <Suspense> to prevent the entire page from becoming dynamic.

Navigation Behavior

Next.js navigation is a client-side transition that:

  1. Prefetches the target route (if not already cached)
  2. Checks the Router Cache for cached segments
  3. Fetches only the changed segments from the server
  4. Renders the new segments while preserving shared layouts
  5. Updates the URL without a full page reload

Shared layouts don't re-render during navigation — only the changed page segments update. This makes navigation feel instant.

Key Interview Distinction

<Link> for declarative navigation with automatic prefetching. useRouter() for programmatic navigation in event handlers. redirect() for server-side redirects in Server Components. Navigation is client-side — shared layouts are preserved, only changed segments re-render. Use usePathname() for active link styling.

Fun Fact

Next.js <Link> component originally required an <a> tag as a child: <Link href='/about'><a>About</a></Link>. This verbose pattern was one of the most common complaints from developers. Next.js 13 finally simplified it — <Link> now renders an <a> tag automatically, matching the HTML you'd expect to write.

Learn These First

Next.js Fundamentals

beginner

File-Based Routing

intermediate

Continue Learning

Server & Client Components

intermediate

Middleware

advanced

Practice What You Learned

How do you navigate between pages in Next.js?
junior
navigation
Next.js provides the `Link` component for declarative client-side navigation and the `useRouter` hook for programmatic navigation, both enabling fast page transitions without full page reloads.
Previous
Middleware
Next
Image & Font Optimization
PrevNext