JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
PrevNext

Learn the concept

Navigation & Linking

nextjs
junior
navigation

How do you navigate between pages in Next.js?

nextjs
navigation
link
useRouter
prefetching
routing
onNavigate
Quick Answer

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.

Detailed Explanation

Navigation Methods:

  1. <Link> Component (preferred)

    • Client-side navigation between routes
    • Automatic prefetching of linked pages
    • No full page reload
    • onNavigate callback (v15.3+) for intercepting client-side navigations
  2. useRouter Hook

    • Programmatic navigation in event handlers
    • Access to route information
    • Methods: push(), replace(), back(), forward()
  3. redirect() Function

    • Server-side redirects in Server Components
    • Use in data fetching or server actions

Prefetching:

  • Links in the viewport are automatically prefetched
  • Disable with prefetch={false}
  • Production only (not in development)

Active Link Styling:

  • Use usePathname() to check current route
  • Apply conditional classes for active state

onNavigate Prop (v15.3+): The onNavigate callback fires during client-side navigation, allowing you to intercept or prevent navigation — useful for unsaved changes warnings or analytics.

Code Examples

Link component usageTSX
import Link from 'next/link';

export default function Navigation() {
  return (
    <nav>
      {/* Basic link */}
      <Link href="/">Home</Link>
      
      {/* Link with dynamic route */}
      <Link href="/posts/123">Post 123</Link>
      
      {/* Link with query params */}
      <Link href="/search?q=nextjs">Search</Link>
      
      {/* Link with object href */}
      <Link
        href={{
          pathname: '/posts/[id]',
          query: { id: '456' },
        }}
      >
        Post 456
      </Link>
      
      {/* Disable prefetching */}
      <Link href="/heavy-page" prefetch={false}>
        Heavy Page
      </Link>
      
      {/* Replace history instead of push */}
      <Link href="/login" replace>
        Login
      </Link>
    </nav>
  );
}

Real-World Applications

Use Cases

SPA-Like Navigation Feel

Use the Link component with prefetching to create instant page transitions in a content-heavy site, giving users an app-like experience without full page reloads.

Breadcrumb Navigation

Build dynamic breadcrumbs using `usePathname()` to parse the current URL segments and render Link components for each level, with active styling for the current page.

Unsaved Changes Guard

Use Link's `onNavigate` callback to warn users about unsaved form data before navigating away, preventing accidental data loss in editing interfaces.

Mini Projects

Navigation Component Library

beginner

Build a set of navigation components — top navbar with active states, sidebar with collapsible sections, breadcrumbs, and pagination — all using Link with prefetching and usePathname for active states.

Multi-Step Wizard with Route Guards

intermediate

Create a multi-step form wizard where each step is a route. Use `onNavigate` to prevent skipping steps and warn about unsaved changes, with `useRouter` for programmatic step advancement.

Industry Examples

Notion

Notion's web app uses client-side navigation for instant page switches between documents, prefetching linked pages for a seamless editing experience.

Linear

Linear uses programmatic navigation with keyboard shortcuts (Cmd+K) to jump between issues, projects, and views without mouse interaction.

Resources

Next.js Link Component

docs

useRouter Hook

docs

Related Questions

How does file-based routing work in Next.js?

junior
routing

What are the key differences between Pages Router and App Router in Next.js?

mid
routing
Previous
How does file-based routing work in Next.js?
Next
What is next/image and why should you use it over the regular img tag?
PrevNext