Course contentsShow
← React
Lesson 1889 of 2,39643. RoutingFree lesson

Navigation with Link and useRouter

Client-side navigation using Next.js Link component and programmatic routing with useRouter hook.

Navigation with Link and useRouter

You'll learn how to navigate between pages in Next.js using both declarative links and programmatic routing.

The Core Idea

Next.js gives you two main tools for moving users between routes: the Link component for clickable navigation and the useRouter hook for JavaScript-driven redirects. Both provide client-side navigation, meaning the page doesn't do a full browser refresh—only the content updates, keeping your app fast and smooth.

Think of it like this: Link is a highway sign that users can click, while useRouter is you taking the wheel and driving them somewhere based on logic.

Using the Link Component

The Link component replaces traditional <a> tags for internal navigation:

import Link from 'next/link';

export default function HomePage() {
  return (
    <div>
      <Link href="/about">About Us</Link>
      <Link href="/posts/123">View Post</Link>
    </div>
  );
}

Next.js automatically prefetches linked pages when they appear in the viewport, making navigation instant.

Programmatic Navigation with useRouter

When you need to navigate based on user actions or logic (after form submission, authentication, etc.), use the useRouter hook:

'use client';
import { useRouter } from 'next/navigation';

export default function LoginButton() {
  const router = useRouter();

  const handleLogin = async () => {
    // Perform login logic...
    router.push('/dashboard'); // Navigate programmatically
  };

  return <button onClick={handleLogin}>Log In</button>;
}

You can also use router.replace() to navigate without adding to browser history, or router.back() to go back one step.

Key Takeaway: Use Link for declarative navigation (user clicks), and useRouter for imperative navigation (code-driven redirects)—both keep navigation fast with client-side routing.