Code To Learn logo

Code To Learn

M4: Routes & Navigation

M4: Routes & Navigation

Build a multi-page application with React Router v6, dynamic routes, and URL parameters.

Transform your single-page app into a multi-page experience with React Router! ๐Ÿš€


Overview

Right now, your app is a single-page application (SPA) - everything happens on one URL. But real websites have multiple pages: home, details, about, etc.

React Router lets you create a multi-page feel while keeping the performance benefits of a SPA. Users can navigate between pages instantly without full page reloads!

In this module, you'll add:

  • A home page (your existing listings page)
  • A details page for each property
  • A 404 Not Found page
  • Smooth navigation between pages

What You'll Build


Key Concepts

Client-Side Routing

Traditional websites reload the entire page on navigation:

User clicks link โ†’ Browser requests new HTML โ†’ Full page reload

React Router changes URLs without page reloads:

User clicks link โ†’ React Router updates URL โ†’ React renders new component

Benefits:

  • โšก Instant navigation (no loading)
  • ๐ŸŽจ Smooth transitions
  • ๐Ÿ’พ Shared state between pages
  • ๐Ÿ“ฑ App-like experience

React Router Setup

import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
   return (
      <BrowserRouter>
         <Routes>
            <Route path="/" element={<HomePage />} />
            <Route path="/property/:id" element={<PropertyDetailsPage />} />
            <Route path="*" element={<NotFoundPage />} />
         </Routes>
      </BrowserRouter>
   );
}

URL Parameters

Extract dynamic values from URLs:

// URL: /property/abc123
import { useParams } from 'react-router-dom';

function PropertyDetailsPage() {
   const { id } = useParams(); // id = "abc123"
   
   // Fetch property with this id
   useEffect(() => {
      fetchProperty(id);
   }, [id]);
}

Link component for navigation (like <a> tags but better):

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

<Link to="/property/abc123">View Details</Link>

useNavigate hook for programmatic navigation:

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

function Component() {
   const navigate = useNavigate();
   
   const handleClick = () => {
      // Do something first
      navigate('/property/abc123');
   };
}

Learning Objectives


Module Roadmap

LessonWhat You'll BuildConcepts Introduced
1-3Router setupBrowserRouter, Routes, Route
4Home routePath and element props
5-8Details pageuseParams, dynamic routes, single item fetch
9Navigation linksLink component
10-11404 pageCatch-all routes, error handling
12-13PolishuseNavigate, breadcrumbs, UX

Route Structure

By the end of this module, your app will have:

/ (root)
โ”œโ”€ HomePage
โ”‚  โ””โ”€ Display all property listings
โ”‚
โ”œโ”€ /property/:id
โ”‚  โ””โ”€ PropertyDetailsPage
โ”‚     โ””โ”€ Show single property details
โ”‚
โ””โ”€ * (catch-all)
   โ””โ”€ NotFoundPage
      โ””โ”€ 404 error message

Example URLs:

https://yourapp.com/                    โ†’ HomePage
https://yourapp.com/property/abc123     โ†’ PropertyDetailsPage (id: abc123)
https://yourapp.com/invalid-page        โ†’ NotFoundPage

Prerequisites

Before starting Module 4, ensure:

  • โœ… Completed Module 3
  • โœ… HomePage fetches and displays listings from API
  • โœ… Understand useEffect and async data fetching
  • โœ… Comfortable with array methods (find, filter)
  • โœ… PropertyCard component displays property information

React Router v6 Features

What's new in v6:

// โœ… v6 (Current) - Cleaner API
<Routes>
   <Route path="/" element={<Home />} />
   <Route path="/about" element={<About />} />
</Routes>

// โŒ v5 (Old) - More verbose
<Switch>
   <Route path="/" component={Home} />
   <Route path="/about" component={About} />
</Switch>

Key improvements:

  • Simpler element prop instead of component
  • Better TypeScript support
  • Nested routes are cleaner
  • Relative paths by default
  • Better bundle size

Use when users click to navigate:

<Link to="/property/123">View Property</Link>

Imperative Navigation (useNavigate)

Use for programmatic navigation after actions:

const navigate = useNavigate();

function handleSave() {
   saveData();
   navigate('/success');
}

Pass data to the next route:

navigate('/property/123', {
   state: { from: 'search-results' }
});

// In PropertyDetailsPage:
const location = useLocation();
console.log(location.state.from); // 'search-results'

Common Patterns

Protected Routes (Preview)

You'll implement this in Module 7:

function ProtectedRoute({ children }) {
   const { user } = useAuth();
   
   if (!user) {
      return <Navigate to="/login" />;
   }
   
   return children;
}

<Route 
   path="/favorites" 
   element={
      <ProtectedRoute>
         <FavoritesPage />
      </ProtectedRoute>
   } 
/>

Layout Routes (Preview)

Share layout across multiple pages:

<Route element={<Layout />}>
   <Route path="/" element={<HomePage />} />
   <Route path="/about" element={<AboutPage />} />
   <Route path="/contact" element={<ContactPage />} />
</Route>

function Layout() {
   return (
      <>
         <Navbar />
         <Outlet /> {/* Child routes render here */}
         <Footer />
      </>
   );
}

Property Details Page Preview

What it will show:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ โ† Back to Listings                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ [Image Carousel]                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Beach House Paradise                โ”‚
โ”‚ โญ 4.8 ยท Miami, USA                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Description:                        โ”‚
โ”‚ Beautiful beachfront property...    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Amenities:                          โ”‚
โ”‚ โ€ข WiFi  โ€ข Pool  โ€ข Kitchen          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Price: $150/night                   โ”‚
โ”‚ Max Guests: 4                       โ”‚
โ”‚ [Book Now Button]                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

URL Design Best Practices

Good URL patterns:

  • / - Homepage
  • /properties - All properties
  • /property/:id - Single property
  • /favorites - User favorites
  • /profile - User profile

Characteristics:

  • Descriptive and readable
  • RESTful conventions
  • Predictable structure
  • Easy to remember

Avoid these patterns:

  • /page1, /page2 - Not descriptive
  • /p?id=123 - Query params for routes (use for filters instead)
  • /propertyDetailsInformation - Too verbose
  • /prop - Too abbreviated

Time Estimate

โฑ๏ธ 3-4 hours to complete all 13 lessons

  • Lessons 1-4 (Router setup): ~45 minutes
  • Lessons 5-8 (Details page): ~1.5 hours
  • Lessons 9-11 (Navigation & 404): ~1 hour
  • Lessons 12-13 (Polish & review): ~45 minutes

What Makes This Module Important

Professional applications need:

  • Multiple pages for different content
  • Shareable URLs for specific items
  • Browser back/forward button support
  • Bookmarkable pages

After this module, you'll understand:

  • How SPAs handle routing
  • URL parameters and dynamic routes
  • Navigation best practices
  • React Router patterns used in production

Let's Begin!

Ready to transform your single-page app into a multi-page experience? Let's start by installing React Router!