Skip to content
EcosystemChapter 34 / 37Intermediate5 min read

Routing with React Router

React Router v7 for beginners. Learn BrowserRouter, Routes, Link, nested routes, Outlet and useParams to build pages in a single page app.

On this page

1. What is Routing with React Router?

A single page app (SPA) has only one HTML page. React Router reads the browser URL and decides which component to render at /about and which at /products/5.

In version 7 everything is imported from one package, react-router. The old react-router-dom package now only re-exports it. In this chapter we cover declarative mode, which is the simplest for a React app built with Vite.

2. Why does it matter?

Without a router you would have to read window.location yourself, show pages with if conditions, and handle the back button.

Without a routerWith React Router
A full page reload on every linkClient-side navigation, instant
Keeping the URL and UI in sync by handThe URL is the source of truth for the UI
You handle the back button yourselfBrowser history works automatically
Getting a dynamic id is harduseParams gives it directly
Layout copied into every pageNested routes and Outlet

3. Where is it used?

Use caseFeature
Multi-page SPARoutes and Route
Product detail pageThe dynamic segment :id and useParams
Dashboard layoutNested routes and Outlet
Redirect after loginuseNavigate or Navigate
Active menu itemNavLink
404 pagepath="*"

4. How does it work?

BrowserRouter listens to the browser history. Routes looks through all its Route children for the one that best matches the current URL, and renders only its element.

From a Link click to the new page
The user clicks a Link
The URL changes, the page does not reload
Routes matches the current URL
The parent route's layout renders
The child route renders in place of Outlet

5. Code example

Setup

bash
npm install react-router

Example 1: Routes, layout and a dynamic page

jsx
import { createRoot } from "react-dom/client";
import { BrowserRouter, Routes, Route, NavLink, Outlet } from "react-router";
import Home from "./Home";
import Products from "./Products";
import ProductDetail from "./ProductDetail";

function Layout() {
  return (
    <div>
      <nav>
        <NavLink to="/">Home</NavLink>
        <NavLink to="/products">Products</NavLink>
      </nav>
      <Outlet />
    </div>
  );
}

createRoot(document.getElementById("root")).render(
  <BrowserRouter>
    <Routes>
      <Route element={<Layout />}>
        <Route index element={<Home />} />
        <Route path="products" element={<Products />} />
        <Route path="products/:id" element={<ProductDetail />} />
        <Route path="*" element={<p>Page not found</p>} />
      </Route>
    </Routes>
  </BrowserRouter>
);

Example 2: useParams and useNavigate

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

export default function ProductDetail() {
  const { id } = useParams();
  const navigate = useNavigate();

  return (
    <section>
      <h1>Product number {id}</h1>
      <button onClick={() => navigate("/products")}>Back to the list</button>
    </section>
  );
}

How does the code work?

PartMeaning
BrowserRouterConnects the app to the browser URL and history
Route element={<Layout />}A layout route with no path, all children go inside it
indexShown at the parent's exact URL (/)
products/:id:id is the dynamic part, available through useParams
OutletThe child route renders here
NavLinkAdds an active class to the active link automatically
path="*"The 404 UI when nothing matches

Interview tip

React Router v7 has three modes: declarative (BrowserRouter), data (loaders and actions with createBrowserRouter), and framework (Vite plugin, file-based routes, SSR). Saying that you pick the mode based on what the project needs is a strong point in an interview.

6. Common mistakes

Mistake: Using an a tag for navigation

Why? A normal anchor tag reloads the whole page and the app's state is lost.

Better: Use Link or NavLink for pages inside the app, and the a tag for outside websites.

Mistake: Creating a nested route but forgetting Outlet

Why? The parent renders but there is no place for the child, so the child page looks empty.

Better: Put an Outlet in each layout route's component, where the child should appear.

Mistake: Calling navigate() during render

Why? Render should be pure, and a side effect causes warnings and loops.

Better: Call navigate() in an event handler, or return the Navigate component from render.

Mistake: Treating a useParams value as a number

Why? URL params are always strings, so comparisons and math go wrong.

Better: Convert with Number(id) when needed and handle invalid values.

7. What to learn before and after

Final quick revision

PointDetail
Packagereact-router (v7)
SetupRoutes and Route inside BrowserRouter
LinksLink and NavLink, no page reload
Dynamic:id and useParams()
LayoutNested Route and Outlet
Navigating from codeuseNavigate()