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.

🛒
UrbanCart Example
Navigating without reloading

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.

Traditional vs SPA Routing
Traditional
Full page reload
vs
SPA (React Router)
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.

terminal Install React Router

This gives you components like BrowserRouter, Routes, Route, Link, and hooks like useNavigate and useParams.

Version note: This lesson covers React Router v6, which is the current standard. Older tutorials might use v5 syntax (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.

App.jsx Basic Router Setup
🧱

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.

What happens
URL: / → renders <Home /> URL: /products → renders <Products /> URL: /cart → renders <Cart /> URL: /checkout → renders <Checkout />

5. useNavigate

Sometimes you need to navigate programmatically — not from a link click, but from code. After a form submission, after adding to cart, after a timeout. The useNavigate hook gives you a function to do this.

AddToCart.jsx Programmatic Navigation
Common use cases
navigate('/checkout') → go to checkout after form submit navigate('/products') → redirect after login navigate(-1) → go back (like browser back button) navigate('/', { replace: true }) → replace history (no back)

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.

App.jsx Dynamic Route with :id
ProductDetail.jsx Reading URL Parameters
🏷️
UrbanCart Example
Product detail pages

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.

App.jsx Layout Route with Outlet

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.

Nested Route Structure
Layout
Navbar + Outlet + Footer
→
Outlet
Renders matched child
→
Home | Products | Cart
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.

NotFound.jsx + App.jsx Catch-All 404 Route
Order matters: Put the * 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.

Note: In a real project, you'd use 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.
Try It Yourself — UrbanCart Simulated Router
Preview

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.

Try It Yourself — Product Detail with State
Preview

Quiz

Question 1 of 5
What does BrowserRouter do in a React Router app?
Question 2 of 5
Why should you use <Link> instead of <a> for internal navigation?
Question 3 of 5
How do you read a URL parameter like /product/42 inside a component?
Question 4 of 5
What does the <Outlet /> component do in a nested route layout?
Question 5 of 5
How do you handle 404 (page not found) in React Router v6?
0
/ 5
Great job!

You completed the React Router quiz.

Next Lesson

useContext & Global State →
You've completed React Fundamentals! Next up: Advanced React patterns.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →