Routing in React

Introduction to Routing

Routing allows single-page applications to update the URL and render different components without a full page reload.

Installation

# Next.js routing comes built-in; no extra install needed 

Basic Routing

// With Next.js App Router
// app/page.tsx (Home)
export default function HomePage() {
  return <h1>Home</h1>
}

// app/about/page.tsx
export default function AboutPage() {
  return <h1>About</h1>
}

Route Parameters

Capture dynamic segments from the URL:

// app/user/[id]/page.tsx
import { useParams } from 'next/navigation';

export default function UserPage() {
  const params = useParams();
  return <p>User ID: {params.id}</p>;
}