Learn how to add multiple pages to your React app and manage complex application state.
Note: React Router requires a build environment, so these are read-only demonstrations. Focus on understanding the patterns — you'll implement them in your projects!
SPA Concept
Routes
Navigation
Dynamic Routes
Protected Routes
State Patterns
Quiz
SPA Concept
Think of a traditional website like switching between different TV sets — each page change loads an entirely new screen from scratch. A Single Page Application (SPA) is like switching channels on ONE TV — the TV stays on, and only the content changes instantly. No reloading, no blank screens.
In a traditional website, every link click asks the server for a whole new HTML page. In a SPA:
The entire app loads once
Clicking links swaps out components instantly (no page reload)
The URL changes, but the browser stays on the same page
Only the data you need is fetched from the server
Traditional vs SPA
Feature
Traditional Website
SPA (React)
Page change
Full reload from server
Instant swap of components
Speed
Slower (each click = new page)
Faster (load once, swap content)
User experience
Flash of white between pages
Smooth, app-like feel
URL
Changes (new page)
Changes (but no reload)
Analogy
Switching TV sets
Switching channels
React Router is the library that makes SPA routing work in React.
Install it with: npm install react-router-dom
Routes
Routes are like a TV channel guide. Channel 1 shows News, Channel 2 shows Sports, Channel 3 shows Movies. In React Router, URL "/" shows the Home component, "/about" shows the About component, "/contact" shows the Contact component.
React Router — Basic Routes (Read-Only)
Route Structure Breakdown
Component
Purpose
<BrowserRouter>
Wraps your entire app — enables routing
<Routes>
Container for all Route definitions
<Route>
Maps a URL path to a component
path="*"
Catches all unmatched URLs (404 page)
Navigation
In a SPA, regular <a> links would cause a full page reload — like unplugging your TV and plugging in a different one. React Router's <Link> component is like pressing buttons on your remote control — it switches the channel without turning off the TV.
React Router — Navigation (Read-Only)
Never use <a href="..."> for internal links in React! It causes a full page reload, which defeats the purpose of a SPA. Always use <Link> or <NavLink> from react-router-dom.
Dynamic Routes
Dynamic routes are like numbered hotel rooms. The URL /room/:id is a template — /room/101, /room/202, and /room/305 all use the same template but show different content based on the room number. The :id part is the variable.
React Router — Dynamic Routes (Read-Only)
Nested Routes
React Router — Nested Routes (Read-Only)
<Outlet /> is like a TV screen inside a cabinet. The cabinet (layout) stays the same, but the screen (Outlet) shows different content depending on which nested route is active.
Protected Routes
Protected routes are like a bouncer at a club. Before letting you into the VIP section (dashboard, profile, settings), the bouncer checks your ID (authentication). If you're not on the list, you get redirected to the entrance (login page).
React Router — Protected Routes (Read-Only)
The replace prop on <Navigate> replaces the current history entry instead of adding a new one. This means pressing "Back" won't take the user back to the protected page they couldn't access.
State Patterns
Managing state in a growing app is like organizing a growing company. A small team can communicate by shouting across the room (props). A medium team needs a bulletin board (Context). A large company needs a formal communication system with departments and protocols (Redux, Zustand, etc.).
As your app grows, you need to choose the right state management approach:
State Management Options
Approach
Best For
Complexity
useState
Local component state (form inputs, toggles)
Simple
useReducer
Complex state logic in one component
Medium
Context + useReducer
Shared state across a few related components
Medium
Zustand
Global state, simple API, lightweight
Low-Medium
Redux Toolkit
Large apps, complex state, team projects
Higher
useReducer — For Complex State
React — useReducer Pattern (Read-Only)
When to Use What
Start simple, upgrade only when needed:
useState — always start here. Handles 80% of cases.
useReducer — when one component has complex state with many actions.
Context — when sibling/distant components need the same state (theme, auth).
Zustand/Redux — when Context becomes unwieldy in a large app.
Don't over-engineer! Many beginners jump to Redux immediately. For most projects, useState + Context is more than enough. Add complexity only when you feel the pain of managing state.
📝 Chapter 8 Quiz
1. What happens when you navigate in a Single Page Application?
The browser loads a new HTML page from the server
React swaps components without a full page reload
The URL stays the same
JavaScript is disabled and re-enabled
2. Why should you use <Link> instead of <a> in React?
Link is faster to type
Link supports CSS styling
Link navigates without a full page reload
Link works in all browsers
3. How do you access URL parameters (like :id) in React Router?
useParams() hook
this.params
window.location.params
getParams() function
4. What does a protected route do?
Encrypts the page content
Hides the URL from the user
Prevents the page from being bookmarked
Redirects unauthenticated users to a login page
5. What's the recommended approach for state management in a new React app?