Routing & Navigation: The Multi-Page SPA
DraftURL: https://ruvebal.github.io/web-atelier-udit/lessons/en/react/react-routing/
π Table of Contents
- π― Sprint Goal
- π Position in Journey
- π§ Learning Objectives
- ποΈ What Weβll Build This Sprint
- π§ Integration Points
- π Methodology: Atelier Practice
- π Sprint Deliverables
- π Lesson Navigation
- π Key Concepts Preview
βRouting is the art of making many pages feel like one app.β
π― Sprint Goal
By the end of this sprint: Transform your app into a full multi-page experience with clean URLs, protected routes, and smooth navigationβwhile remaining a single-page application.
π Position in Journey
| Sprint | Focus | Your App Grows |
|---|---|---|
| 5. Fundamentals | Components, JSX, Props | Component library skeleton |
| 6. Hooks | State & effects | Interactive components |
| 7. Architecture | Global state | Connected features |
| β 8. Routing | Navigation | Multi-page structure |
π§ Learning Objectives
By the end of this lesson, you will:
- Set up React Router v6 in your project
- Create declarative routes with
<Routes>and<Route> - Navigate programmatically with
useNavigate - Read URL parameters with
useParamsanduseSearchParams - Implement nested routes and layouts with
<Outlet> - Protect routes based on authentication state
ποΈ What Weβll Build This Sprint
Route Structure for Your App
// Example route structure:
<Routes>
{/* Public routes */}
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="products" element={<ProductList />} />
<Route path="products/:id" element={<ProductDetail />} />
</Route>
{/* Auth routes */}
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
{/* Protected routes */}
<Route element={<ProtectedRoute />}>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/profile" element={<Profile />} />
<Route path="/settings" element={<Settings />} />
</Route>
{/* Catch-all */}
<Route path="*" element={<NotFound />} />
</Routes>
π§ Integration Points
| Data Source | Routing Integration |
|---|---|
| Laravel API | Dynamic routes from API (e.g., /products/:slug) |
| Hygraph CMS | Content-driven routes (blog posts, pages) |
| Auth State | Protected routes check AuthContext |
URL as State
// Filters in URL = shareable, bookmarkable UI state
// Instead of:
const [filter, setFilter] = useState('all');
// Use:
const [searchParams, setSearchParams] = useSearchParams();
const filter = searchParams.get('filter') || 'all';
// URL: /products?filter=electronics&sort=price
π Methodology: Atelier Practice
The Sprint Rhythm
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DAY 1: Basic Routing β
β β’ Set up React Router in your project β
β β’ Create public routes and layouts β
β β’ Practice: Navigation between your existing pages β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β DAY 2: Dynamic & Protected Routes β
β β’ Build product/article detail pages with useParams β
β β’ Implement ProtectedRoute wrapper β
β β’ Wire to your AuthContext from Sprint 7 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β DAY 3: Polish & Edge Cases β
β β’ Add loading states during navigation β
β β’ Handle 404 with a beautiful NotFound page β
β β’ Test: Can users deep-link? Bookmark? Refresh? β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
AI-Assisted Development Protocol
| Task | AI Role | Your Role |
|---|---|---|
| Generate route structure | Propose based on your app | Verify navigation logic |
| Implement ProtectedRoute | Scaffold the wrapper | Test edge cases (no token, expired) |
| Handle nested layouts | Show Outlet pattern | Apply to your structure |
| Debug navigation issues | Explain common pitfalls | Fix and test |
π Sprint Deliverables
- 5+ routes covering your appβs main pages
- Nested layout with shared header/footer
- Dynamic route with URL parameter (e.g.,
:id) - Protected route requiring authentication
- 404 page with helpful navigation
- URL-based filtering on at least one list page
- Reflection: How does URL state change your UX thinking?
π Lesson Navigation
| Previous | Current | Next |
|---|---|---|
| State Architecture | Routing | Backend Integration |
π Key Concepts Preview
URL state is real state
- Filters, pagination, and selected entities often belong in the URL.
- Treat the URL as a shareable snapshot of app state.
Practical patterns
- Nested layouts: keep nav + shell stable while pages change
- Protected routes: authorization gate, not just βredirect magicβ
- Search params: the simplest persistence for filters
Example: filters as URL state (conceptual)
// Example state you can encode in the URL:
// /products?query=shoes&sort=price&page=2
Reflection (Atelier)
π Which state should be shareable via link? Which state should not? Why?
π What breaks when you refresh the page? What does that reveal about your architecture?
Koan
βIf the URL lies, the user will not return.β
βA URL is a promise to the user: this address will always show this content.β