1. What is Routing
Traditional websites reload the entire page every time you click a link. Click "Products" — the browser fetches a whole new HTML document. Click "Cart" — another full reload. Every navigation is a round trip to the server.
Single Page Applications (SPAs) work differently. The browser loads one HTML file, and JavaScript handles everything else. When you click "Products," React swaps out the component on screen — the URL changes, but the page never actually reloads. This is client-side routing.
When a customer browses UrbanCart, they move between Home, Products, Cart, and Checkout. With React Router, clicking between these pages is instant — no white-screen flash, no waiting for the server. The URL updates to /products or /cart, the browser history works (back button!), and React simply renders the right component.
Full page reload
Swap component, update URL
2. react-router-dom
React doesn't include routing out of the box. You install react-router-dom, the most popular routing library for React web apps.
This gives you components like BrowserRouter, Routes, Route, Link, and hooks like useNavigate and useParams.
Switch instead of Routes, component instead of element). If you see that, you're reading outdated code.
3. BrowserRouter, Routes, Route
Every React Router app has three layers: a router that wraps everything, a Routes container, and individual Route definitions that map URLs to components.
BrowserRouter
Wraps your entire app. It uses the browser's History API to keep the URL in sync with what's on screen.
Routes + Route
Routes is the container. Each Route maps a path to a React element. Only the matching route renders.
6. useParams
UrbanCart has hundreds of products. You can't create a separate route for each one. Instead, you use dynamic routes with URL parameters.
When a customer clicks on "Wireless Headphones" in the UrbanCart product list, the app navigates to /product/7. The ProductDetail component reads id = "7" from the URL using useParams(), fetches the product data from the API, and renders the detail page. One component, infinite products.
7. Nested Routes
Most apps have a shared layout — a navbar that appears on every page, a sidebar that shows on some pages. Nested routes let you define a layout route that wraps child routes.
The <Outlet /> component is the placeholder. It says "render whatever child route matches here." The Layout component renders Navbar and Footer on every page, and the middle content changes based on the URL.
Navbar + Outlet + Footer
Renders matched child
Swaps based on URL
8. 404 Page
What happens when a user types /asdfghjkl in the URL? Without a catch-all route, they see a blank page. Add a path="*" route at the end to handle unmatched URLs.
* route last. React Router matches routes top-to-bottom in v6 using a scoring system, but placing it last makes your intent clear and avoids surprises.
9. Try It Yourself — Simulated Router
React Router requires npm install and a build tool, so it can't run in a browser-only editor. Instead, we'll simulate routing with useState — the concept is identical: render different components based on the current "page" state.
react-router-dom with npm install. Here we simulate routing with useState to demonstrate the concept. The patterns are the same — components render based on the current "page" state, and the nav bar highlights the active page.
10. Try It Yourself — Product Detail View
This example simulates useParams — clicking a product shows its detail page, and the back button returns to the list. In a real app, useParams reads the product ID from the URL. Here, we store it in state.
Quiz
BrowserRouter do in a React Router app?<Link> instead of <a> for internal navigation?/product/42 inside a component?<Outlet /> component do in a nested route layout?You completed the React Router quiz.
You just built multi-page navigation in React.
Now build a full-stack career that goes places.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →