052
LVL 02 — CIRCUIT BREAKER SESSION 052 DAY 52

REACT ROUTER

🎫 PIXELCRAFT-040
Feature | 🟡 Medium | Priority: 🟠 High

Everything is on one page. We need: /editor/:imageId for editing, /gallery for browsing, /settings for preferences. URLs should be shareable.
CONCEPTS.UNLOCKED
🛤
React Router
Client-side routing with react-router-dom. Map URLs to components. Navigate between pages without a full page reload. The browser URL reflects the current view.
🗺
Routes
<Route path="/gallery" element={<Gallery />} /> — when the URL matches /gallery, render the Gallery component. Declarative routing: describe the mapping, Router handles the rest.
🔑
URL Parameters
/editor/:imageId → useParams() extracts imageId. Deep linking: share a URL → the exact image opens. Each image has its own permanent, shareable URL.
🔗
Navigation
<Link to="/gallery"> for declarative navigation. useNavigate() for programmatic navigation (e.g., after saving). No page reloads — instant transitions.
📐
Layout Routes
Shared sidebar/header across pages. The Layout component renders the navigation + an <Outlet /> where child routes appear. Structure that persists across page changes.
💤
Lazy Loading
React.lazy() + <Suspense> — load page code only when needed. The Editor is heavy; don't load it until the user navigates there. Faster initial load.
HANDS-ON.TASKS
01
Install and Set Up React Router
npm install react-router-dom
02
Define Routes
function App() { return ( <BrowserRouter> <Routes> <Route element={<Layout />}> <Route path="/" element={<Navigate to="/gallery" />} /> <Route path="/gallery" element={<Gallery />} /> <Route path="/editor/:imageId" element={<Editor />} /> <Route path="/settings" element={<Settings />} /> <Route path="*" element={<NotFound />} /> </Route> </Routes> </BrowserRouter> ); }
03
Build the Layout with Shared Navigation
function Layout() { return ( <div className="flex h-screen"> <nav className="w-14 bg-slate-900 flex flex-col items-center py-4 gap-2"> <Link to="/gallery" className="...">🖼️</Link> <Link to="/editor" className="...">🎨</Link> <Link to="/settings" className="...">⚙️</Link> </nav> <main className="flex-1"> <Outlet /> {/* Child route renders here */} </main> </div> ); }
The Layout persists across page changes — only the <Outlet /> content swaps. Navigation sidebar stays, page content changes. No flicker, no reload.
04
Deep Linking & Programmatic Navigation
// Image deep linking: function Editor() { const { imageId } = useParams(); // Load image by ID } // Programmatic navigation: const navigate = useNavigate(); const openImage = (id) => navigate(`/editor/${id}`);
05
Lazy Load the Editor
const Editor = React.lazy( () => import('./pages/Editor') ); <Suspense fallback={<LoadingSpinner />}> <Route path="/editor/:imageId" element={<Editor />} /> </Suspense>
The Editor bundle is only downloaded when the user navigates to /editor. Gallery users never load Editor code. Initial page load is significantly faster.
06
Close the Ticket
git switch -c feature/PIXELCRAFT-040-routing git add src/ git commit -m "Add React Router with gallery, editor, settings pages (PIXELCRAFT-040)" git push origin feature/PIXELCRAFT-040-routing # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Routing maps URLs to code.

Routing is a universal dispatch pattern: receive a request → determine the handler → execute.

// Routing everywhere:

Express        → HTTP requests →
                 handlers
Django         → URLs → views
Next.js        → files → routes
OS syscalls    → calls → kernel functions
Network routers → packets →
                 destinations
Event buses    → messages →
                 handlers
"Navigation Lab"
[A] Add a 404 page with a link back to gallery. Style it with a fun illustration. Test by navigating to /nonexistent-page.
[B] Add URL query parameters for gallery filters: /gallery?sort=date&tag=landscape. Parse with useSearchParams() and apply to the gallery view.
[C] Add route guards: /editor requires an image to be loaded. If no image, redirect to /gallery with a toast message. Research the concept of "protected routes."
REF.MATERIAL
ARTICLE
Remix / React Router Team
Official tutorial: setup, routes, params, nested routes, layout routes, loaders, and the latest React Router patterns.
ROUTEROFFICIALESSENTIAL
VIDEO
Web Dev Simplified
Practical React Router walkthrough: routes, params, navigation, layout routes, nested routes, and lazy loading.
ROUTERPRACTICAL
ARTICLE
React Team
Official lazy loading reference: dynamic imports, Suspense boundaries, fallback UI, and code splitting for better performance.
LAZYSUSPENSEOFFICIAL
VIDEO
Fireship
Rapid overview: client-side routing, route definitions, params, nested routes, and why SPAs need a router.
ROUTERQUICK
ARTICLE
Wikipedia
The architecture behind client-side routing: history API, hash routing, progressive enhancement, and SPA vs MPA tradeoffs.
SPAARCHITECTURE
// LEAVE EXCITED BECAUSE
PixelCraft has real pages! Share a URL → exact image opens. Navigate → gallery remembers your position. It feels like a professional multi-page app.