React Router Basics

React Router does client-side routing: the URL changes and the component that shows switches, without loading the page again (a reload). <BrowserRouter> wraps…

In an online shop app, you click "About us", then "Products", then the Back button. The URL in the address bar changes every time, and the page switches almost instantly, with no white screen in between.

Choosing which page shows based on the URL is called routing. In a React app, routing happens in the browser (client-side routing). The URL changes and the page content switches, without asking the server for new HTML and without loading the page again (a reload). React Router is the most popular library for it. Client-side routing is like moving between rooms inside one house. You just walk to another room, and you see a different sign on its door. You don't need to go out through the gate and come back in. The house is your app, each room is one page, the sign is the URL, and going out and back in through the gate is a reload.

The URL decides the component

Here is an app with four pages. Each <Route> pairs one address (path) with one component (element). That pair is called a route:

import { BrowserRouter, Routes, Route } from "react-router"

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Layout />}>
          <Route index element={<Home />} />
          <Route path="about" element={<About />} />
          <Route path="users/:id" element={<UserDetail />} />
          <Route path="*" element={<NotFound />} />
        </Route>
      </Routes>
    </BrowserRouter>
  )
}
URL in the address bar Matching route What shows inside Layout
/ index Home
/about about About
/users/2 users/:id, with id = "2" UserDetail
/contact * NotFound

Moving between pages with Link

To move between pages inside the app, use <Link>, not an <a> tag:

import { Link } from "react-router"

function Nav() {
  return (
    <nav>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
      <Link to="/users/2">User 2</Link>
    </nav>
  )
}

On the page, <Link to="/about"> still becomes an <a href="/about"> tag. The difference is that React Router catches the click with JavaScript. The URL is swapped, then <Routes> picks the new component. No HTML request goes to the server, so the existing state is kept.

Now compare a plain <a href>. Say there's a Like button that keeps the number of likes in state. You press it until it reads 3, then click <a href="/about">. Take a guess: what does Like read now?

Like goes from 3 back to 0. The browser asks the server for a new page, the whole app loads from zero, and all state in memory is lost. Use <a href> only for links to other sites.

To change page from code, for example after a button is clicked, use useNavigate:

import { useNavigate } from "react-router"

function NextButton() {
  const navigate = useNavigate()
  return <button onClick={() => navigate("/about")}>Go to About</button>
}

navigate("/about") changes the URL, the same as clicking <Link to="/about">.

Parameters in the URL: useParams

The UserDetail component shows for the route users/:id. It reads id from the URL:

import { useNavigate, useParams } from "react-router"

const USERS = { 1: "Budi", 2: "Ani" }

function UserDetail() {
  const { id } = useParams() // at /users/2, id = "2"
  const navigate = useNavigate()

  return (
    <div>
      <h1>User #{id}</h1>
      <p>{USERS[id] ?? "No such user"}</p>
      <button onClick={() => navigate(`/users/${Number(id) + 1}`)}>Next user</button>
    </div>
  )
}

A shared layout: nested routes and Outlet

In the first example, the about and users/:id routes are written inside <Route path="/" element={<Layout />}>. A route inside another route is called a nested route, or a child route. Because of that, Layout always shows, and the page of the child route appears where <Outlet /> sits. The Layout below also keeps a likes state for the Like button:

import { Outlet } from "react-router"

function Layout() {
  const [likes, setLikes] = useState(0)

  return (
    <div>
      <Nav />
      <button onClick={() => setLikes(likes + 1)}>Like {likes}</button>
      <main>
        <Outlet />
      </main>
    </div>
  )
}

Going from / to /about through a <Link> only swaps what's in <Outlet />. Layout isn't unmounted, so likes stays. If you forget to write <Outlet />, the child route's page has nowhere to go, so only Layout shows.

In the exercises: real React Router

The exercise editor loads the real React Router (version 6.30.1) as soon as your code imports "react-router-dom" or "react-router". Everything used in these exercises is the same in v6 and v7. There's one difference: the preview has no address bar. So BrowserRouter there keeps its address history in memory and always starts at /.

React Router is now at version 8 (since June 2026). The code in this lesson still runs on v8. But v8 removed the react-router-dom package, so import from "react-router".

Other options: React Router has three modes (ways to use it). This lesson uses Declarative mode, because it has the fewest ideas to learn.

Mode When to pick it
Declarative: <BrowserRouter> and <Routes> A browser app that only needs to pick a page from the URL.
Data: createBrowserRouter and <RouterProvider> You want a loader (a function that fetches the data before the page shows) and an action (a function that handles a form that was sent).
Framework: npx create-react-router@latest A full new app. The React Router docs suggest this mode if you have no preference yet.

There is also TanStack Router, another router that checks addresses and their parameters with TypeScript (JavaScript with data types added).

⚠️ Links that reload the page, and child pages that never show

TL;DR: React Router does client-side routing: the URL changes and the component that shows switches, without loading the page again (a reload). <BrowserRouter> wraps the app. <Routes> picks the one <Route path="..." element={...}> that matches the URL. index matches the exact address of the route that wraps it, and * matches the other addresses that have no route. Change page with <Link to="/about">, not <a href> (which reloads the page and wipes the state), or from code with useNavigate(). A :id in the path is read with const { id } = useParams(), and its value is always a string. Routes written inside another route share one layout (a part that always shows), and their page shows where <Outlet /> sits. In v7 everything is imported from "react-router".

⚡ The URL picks the component

Write:

<BrowserRouter>
  <Routes>
    <Route path="/" element={<Layout />}>
      <Route index element={<Home />} />
      <Route path="about" element={<About />} />
      <Route path="users/:id" element={<UserDetail />} />
      <Route path="*" element={<NotFound />} />
    </Route>
  </Routes>
</BrowserRouter>

Meaning: Each <Route> pairs one address (path) with one component (element). <BrowserRouter> reads the URL, then <Routes> looks for the one <Route> that matches the URL and shows the component in its element. index matches the exact address of the route that wraps it, here /. :id may hold anything, and * matches every address that no other route matches.

⚡ Link, not a href

Write:

<Link to="/about">About</Link>
Guess first: a Like button keeps its number in state, and it reads 3. Then you click a plain <a href="/about">, not a Link. What does Like read now?

Back to 0. The browser asks the server for a new page, the whole app loads from zero, and all state is lost.

Meaning: <Link to="/about"> still becomes an <a> tag on the page, but React Router catches the click. The URL changes without loading the page again (a reload), so state is kept. That's client-side routing: the page is picked in the browser, not asked for again from the server.

⚡ Parameters in the URL

Write:

const { id } = useParams() // at /users/2, id = "2"

Meaning: For the route users/:id at the address /users/2, useParams() returns { id: "2" }. The value is always a string, so id + 1 gives "21". Convert it with Number(id) before doing maths.

What you will learn