React Router: Client-Side Navigation

Build fast, multi-page web applications without ever leaving the page. Welcome to the world of client-side routing.

Lesson ProgressStep 1 of 8
🚀
0 EXP

Welcome! Let's build a Single-Page App (SPA). First, we import the core components from 'react-router-dom'.

import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

Core Concepts: `BrowserRouter`, `Routes`, & `Route`

To start, you must wrap your entire app in <BrowserRouter>. This component uses the HTML5 History API to keep your UI in sync with the URL.

Inside your app, you use <Routes> to contain all your route definitions. This component is smart: it will only render the *first* child <Route> that matches the current URL.

Finally, the <Route> component does the actual mapping. It takes two key props:

  • `path`: The URL path to match (e.g., `"/about"`).
  • `element`: The React element to render (e.g., `<AboutPage />`).

System Check

Which component is responsible for defining a single page, mapping a path to a component?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🚀
Router Ready

Correctly wrap your app in <BrowserRouter> and define a <Route>.

🧭
Navigator

Correctly use <Link> for navigation and <Routes> to group routes.

🔗
Dynamic Handler

Create a dynamic route with a URL parameter.

Mission: Build a Mini-App

Create a component with a `<Link>` to the homepage ("/") and a `<Route>` for the homepage that renders an `<h1>`. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Components

A router's structure is critical. Drag the components into the correct nested order.

<Route path='/' element={<Home />} />
<BrowserRouter>
</Routes>
<Routes>
</BrowserRouter>

Challenge: Complete the Syntax

Fill in the missing prop names to build a valid route and link.

<Link ="/users" /><Route ="/users" ={<UsersPage />} />

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Mini-App" project for feedback from other Net-Runners.

Beyond the Link: A Deep Dive into React Router

React Router is the standard library for routing in React. It allows you to build a **Single-Page Application (SPA)**—a web app that loads a single HTML page and dynamically updates content as the user interacts with it. This approach is much faster than traditional "multi-page applications" because it avoids full-page reloads.

At its core, React Router's job is to keep your UI in sync with the browser's URL. Let's explore the essential components and hooks from React Router v6.

The Core Component Hierarchy

To get started, you need to understand four main components that work together:

  • <BrowserRouter>: You wrap your *entire application* in this component. It connects your app to the browser's URL using the HTML5 history API, allowing it to read and change the URL.
  • <Routes>: This component is the successor to `<Switch>`. You wrap all your individual routes within it. It intelligently looks at the current URL and renders the *best* matching child `<Route>`.
  • <Route>: This is the component that defines a single route. It has two main props:
    • `path`: A string (like `"/about"`) that matches a URL segment.
    • `element`: The React component to render when the path matches (like `<AboutPage />`).
  • <Link>: This is your navigation tool. It renders an `<a>` tag, but it intercepts the click. Instead of reloading the page, it just changes the URL, letting the `<Routes>` component handle the rest.
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>

      <Routes>
        <Route path="/" element={<h2>Home Page</h2>} />
        <Route path="/about" element={<h2>About Page</h2>} />
      </Routes>
    </BrowserRouter>
  );
}

Handling Dynamic Data with Hooks

Static pages are great, but most apps need dynamic content, like user profiles or product details. React Router handles this with URL parameters and hooks.

1. URL Parameters & `useParams()`

You can define a "dynamic segment" in your route's path using a colon (`:`). For example, `path="/users/:userId"`. This will match URLs like `/users/123` or `/users/alice`.

Inside your component (e.g., `UserProfilePage`), you use the useParams() hook to access the value of that dynamic segment.

// In your <Routes>
<Route path="/users/:userId" element={<UserProfile />} />

// In UserProfile.js
import { useParams } from 'react-router-dom';

function UserProfile() {
  let { userId } = useParams();
  // If URL is "/users/123", userId will be "123"
  return <h2>User Profile: {userId}</h2>;
}

2. Programmatic Navigation & `useNavigate()`

What if you need to redirect a user after they submit a form or log in? You can't use a `<Link>` for that. Instead, you use the useNavigate() hook.

Calling `useNavigate()` gives you a function (conventionally called `Maps`) that you can call to send the user to a new URL.

import { useNavigate } from 'react-router-dom';

function LoginForm() {
  let navigate = useNavigate();

  const handleSubmit = (event) => {
    event.preventDefault();
    // ... (your login logic)
    // On success, redirect to the dashboard:
    navigate("/dashboard");
  };

  return <form onSubmit={handleSubmit}>...</form>;
}

Advanced Pattern: Nested Routes & `<Outlet />`

This is one of the most powerful features of v6. Nested routes allow you to create layouts that share common UI (like a sidebar or dashboard header) while changing the main content.

You define child routes by nesting `<Route>` components. The parent route component then uses the <Outlet /> component to specify *where* its child routes should be rendered.

Route Definitions (App.js)

<Routes>
  <Route path="/dashboard" element={<Dashboard />}>
    <Route path="profile" element={<Profile />} />
    <Route path="settings" element={<Settings />} />
  </Route>
</Routes>

Parent Component (Dashboard.js)

import { Outlet, Link } from 'react-router-dom';

function Dashboard() {
  return (
    <div>
      <h1>Dashboard</h1>
      <nav>
        <Link to="profile">Profile</Link>
        <Link to="settings">Settings</Link>
      </nav>
      <hr />
      <Outlet /> {/* Child renders here */}
    </div>
  );
}

When a user visits `/dashboard/profile`, React Router will render the `Dashboard` component, and inside its `<Outlet />`, it will render the `Profile` component.

Key Takeaway: React Router v6 provides a powerful, hook-based API to manage all aspects of navigation. By mastering `Routes`, `Route`, `Link`, and the `useParams`, `useNavigate`, and `Outlet` tools, you can build complex, fast, and user-friendly SPAs.

React Router Glossary

Single-Page Application (SPA)
A web application that interacts with the user by dynamically rewriting the current web page with new data from the server, instead of the default method of the browser loading entire new pages.
Client-Side Routing
The process of handling URL changes in the browser (the "client") using JavaScript, without making a request to the server for a new HTML page.
`<BrowserRouter>`
The primary component that wraps your application. It uses the browser's HTML5 History API (pushState, replaceState) to keep your UI in sync with the URL.
`<Routes>`
A component that acts as a container for all your individual `<Route>` elements. It intelligently searches its children to find the best match for the current URL.
`<Route>`
Defines the mapping between a URL path and the React component that should be rendered. Its main props are `path` and `element`.
`<Link>`
The component used for navigation. It renders an `<a>` tag but prevents the default page reload, allowing `<Routes>` to take over.
`<NavLink>`
A special version of `<Link>` that "knows" whether it is active or not. This is useful for styling the active navigation link (e.g., adding an "active" CSS class).
`useParams()`
A hook that returns an object of key/value pairs from the URL's dynamic segments. Used for accessing parameters like `:id` from the path.
`useNavigate()`
A hook that returns a function, allowing you to programmatically navigate to a new URL (e.g., after a form submission or login).
`useLocation()`
A hook that returns the current "location" object, which contains information about the current URL, including its `pathname`, `search` (query params), and `hash`.
`useSearchParams()`
A hook used to read and modify the query string (e.g., `?search=...`) in the URL.
`<Outlet />`
A component used within parent routes to render their child routes. It acts as a placeholder for nested UI.
Protected Route
A common pattern (not a specific component) where you create a custom route wrapper that checks for authentication. If the user is logged in, it renders the requested component; otherwise, it redirects them to a login page.

About the Author

Author's Avatar

TodoTutorial Team

Expert React developers and educators passionate about making modern web accessible.

This article was written and reviewed by our team of senior React developers who have built and maintained large-scale SPAs using React Router.

Verification and Updates

Last reviewed: November 2025.

This content is verified against **React Router v6.x**, the current stable, hook-based API. All examples are tested and reflect modern best practices.

External Resources

Found an error or have a suggestion? Contact us!