Lesson 7 • Intermediate
React Router ⚛️
Add multi-page navigation to your React app — define routes, handle dynamic URLs, and protect pages with authentication guards.
What You'll Learn in This Lesson
- • Setting up React Router with
BrowserRouter - • Link, NavLink, and programmatic navigation
- • Dynamic routes with
useParams - • Query parameters with
useSearchParams - • Nested routes, Outlet, and protected routes
react-router-dom in a Vite project.1️⃣ Setup & Basic Routes
React Router maps URL paths to components. Wrap your app in BrowserRouter, then define routes with <Route path="..." element={<Component />} />. Use path="*" for a 404 page.
Try It: Basic Routing
Set up routes and see how URL matching works
// React Router — Setup & Basic Routes
console.log("=== Installing React Router ===");
console.log("npm install react-router-dom");
console.log();
console.log("=== Basic Setup (main.jsx) ===");
console.log("import { BrowserRouter } from 'react-router-dom';");
console.log();
console.log("ReactDOM.createRoot(root).render(");
console.log(" <BrowserRouter>");
console.log(" <App />");
console.log(" </BrowserRouter>");
console.log(");");
console.log();
console.log("=== Defining Routes (App.jsx)
...2️⃣ Navigation
Use Link instead of <a> tags for client-side navigation. NavLink adds active styling automatically, and useNavigate enables programmatic redirects (e.g., after login).
3️⃣ Dynamic Routes & Parameters
Dynamic segments like :userId capture values from the URL. Read them with useParams(). For query strings (?q=react), use useSearchParams().
Try It: Dynamic Routes
URL parameters and query strings
// Dynamic Routes & URL Parameters
console.log("=== Dynamic Segments ===");
console.log("// URL parameters with :paramName");
console.log("<Route path='/users/:userId' element={<UserProfile />} />");
console.log("<Route path='/products/:category/:id' element={<Product />} />");
console.log();
console.log("=== Reading URL Params (useParams) ===");
console.log("import { useParams } from 'react-router-dom';");
console.log();
console.log("function UserProfile() {");
console.log(" const { userId }
...4️⃣ Nested & Protected Routes
Nested routes let you share layout across pages using Outlet. Protected routes check authentication before rendering — redirecting unauthenticated users to the login page.
Try It: Nested & Protected Routes
Layout sharing and auth guards
// Nested Routes & Protected Routes
console.log("=== Nested Routes ===");
console.log("// Parent route with <Outlet /> for child content");
console.log();
console.log("<Route path='/dashboard' element={<DashboardLayout />}>");
console.log(" <Route index element={<DashboardHome />} />");
console.log(" <Route path='settings' element={<Settings />} />");
console.log(" <Route path='profile' element={<Profile />} />");
console.log("</Route>");
console.log();
console.log("// DashboardLayout.jsx:");
...⚠️ Common Mistakes
<a> instead of Link — Regular anchor tags cause full page reloads, losing all React state.Outlet — Nested routes won't render child content without an Outlet in the parent layout.navigate('/path', { replace: true }) after login so users can't "back" into the login page.📋 Quick Reference
| Feature | Syntax |
|---|---|
| Route | <Route path="/about" element={<About />} /> |
| Link | <Link to="/about">About</Link> |
| URL param | const { id } = useParams() |
| Navigate | const navigate = useNavigate() |
| Outlet | <Outlet /> in parent layout |
🎉 Lesson Complete!
You can now build multi-page React apps with routing, dynamic URLs, and protected pages. Next, let's put everything together in a complete project!
Sign up for free to track which lessons you've completed and get learning reminders.