Single Page Application Architecture 45 min interactive guide⚡ Live React Router Simulator

React Routing: React Router DOM, Dynamic Routes & Layouts

Master client-side routing in modern React. Learn BrowserRouter, Routes, Route, Link vs NavLink, dynamic route parameters with useParams, nested layouts with Outlet, query search params, 404 fallback handling, and protected routes.

01

1. What Is Routing & Client-Side Routing?

In traditional multi-page websites, every link click triggers an HTTP request to the web server, which sends back an entirely new HTML document—causing the entire browser screen to blink and reload.

In a React Single-Page Application (SPA), Client-Side Routing intercepts browser URL changes using the HTML5 History API (pushState) and dynamically mounts the corresponding React component—without requesting a new HTML page!

🚀 The Benefit: Instant screen transitions, preserved global application state, reduced bandwidth usage, and a seamless native-app feel.
03

3. Setting Up React Router DOM

Install the official package from npm:

npm install react-router-dom

Wrap your root application with <BrowserRouter> and define routes inside <Routes>:

import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { Home } from './pages/Home';
import { About } from './pages/About';

export function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}
05

5. Navigation: Link, NavLink & useNavigate

ToolUsagePrimary Purpose
<Link to="/path">Declarative in JSXClient-side link navigation without browser reload
<NavLink to="/path">Active-styled navigationProvides isActive boolean to highlight current tab
useNavigate()Programmatic in functionsImperative redirect after login, submit, or timer
import { Link, NavLink, useNavigate } from 'react-router-dom';

export function Navbar() {
  const navigate = useNavigate();

  return (
    <nav>
      <NavLink to="/" className={({ isActive }) => isActive ? 'active' : ''}>Home</NavLink>
      <Link to="/about">About</Link>
      <button onClick={() => navigate('/dashboard')}>Go to Dashboard</button>
    </nav>
  );
}
06

6. Route Parameters & useParams

Use a colon : to define dynamic parameters in your route path. In the target component, read the values using useParams():

// App.jsx
<Route path="/products/:productId" element={<ProductDetail />} />

// ProductDetail.jsx
import { useParams } from 'react-router-dom';

export function ProductDetail() {
  const { productId } = useParams();
  return <h2>Showing details for product #{productId}</h2>;
}
07

7. Nested Routes, Layouts & <Outlet />

Nested routing allows child components to share a parent layout (e.g. persistent sidebar, navigation bar) without re-mounting the layout on page change:

// App.jsx
<Route path="/dashboard" element={<DashboardLayout />}>
  <Route index element={<DashboardOverview />} />
  <Route path="settings" element={<Settings />} />
  <Route path="profile" element={<Profile />} />
</Route>

// DashboardLayout.jsx
import { Outlet, Link } from 'react-router-dom';

export function DashboardLayout() {
  return (
    <div className="dashboard">
      <aside>
        <Link to="/dashboard">Overview</Link>
        <Link to="/dashboard/settings">Settings</Link>
      </aside>
      <main>
        <Outlet />
      </main>
    </div>
  );
}
09

9. Handling 404 Fallback Pages

Use the wildcard path="*" at the bottom of your route definitions to capture any URL that does not match predefined rules:

<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/about" element={<About />} />
  <Route path="*" element={<NotFound />} />
</Routes>
10

10. Query Parameters with useSearchParams

Query strings like /courses?category=react&sort=asc are read and modified using useSearchParams():

import { useSearchParams } from 'react-router-dom';

export function CourseFilter() {
  const [searchParams, setSearchParams] = useSearchParams();
  const category = searchParams.get("category") || "all";

  return (
    <button onClick={() => setSearchParams({ category: "react" })}>
      Filter React (Current: {category})
    </button>
  );
}
12

12. Protected Routes (Authentication Guards)

Guard private pages (like dashboards or admin consoles) from unauthenticated users:

import { Navigate, Outlet } from 'react-router-dom';

export function ProtectedRoute({ isAuthenticated }) {
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />;
  }
  return <Outlet />;
}
14

14. Common Mistakes & Best Practices

  • Using <a href> instead of <Link to>: Causes full page reloads and state resets.
  • Forgetting <Outlet /> in nested layouts: Child components will fail to display.
  • Leading slash in nested child paths: Child routes under /dashboard should be defined as path="settings" (relative), not path="/settings".
  • Organize route trees hierarchically: Group related routes under shared layouts.
LIVE INTERACTIVE LAB

React Router Playground & State Inspector

Experience client-side single-page routing! Click navigation links or type custom paths in the simulated browser URL bar to watch route matching, parameter extraction, and component rendering live.

1. Navigation Action2. URL pushState3. Route Pattern Match4. useParams Extraction5. <Outlet /> Rendered View
<NavLink>:
Active Component: <HomePage />Rendered

Welcome to TechStore

This is the index route component (<HomePage />). Explore our courses, developer tools, and dynamic product catalogs!

React Router Engine State
// Current Router State:
{ "currentUrl": "/", "matchedPattern": "/", "renderedComponent": "<HomePage />", "useParams": {}, "isNestedLayout": false }
🎯 React Router Challenge 1 of 4❌ Try Again

Goal 1: Navigate to the /about route using the mini navbar or URL bar.

TEST YOUR KNOWLEDGE

React Router & SPA Routing Quiz

8 scenario-based questions testing your understanding of client-side routing, BrowserRouter, Link vs NavLink, dynamic route parameters, nested layouts with Outlet, and 404 fallbacks.

Question 1 of 8Score: 0 / 8
🌐 How does Client-Side Routing in a React Single-Page Application (SPA) work?