Hello! Welcome to the first lesson in our module on building multi-page applications with React.
In the last module, you did a fantastic job building a mini-app that fetches and filters data from an API. You now have a solid, interactive component, but it exists on a single "page." Real-world applications, especially the e-commerce site you're aiming to build, require multiple views: a home page, product listings, individual product details, a shopping cart, and a checkout flow.
This lesson addresses your first learning outcome in this new module: Install and configure React Router to enable navigation in a single-page application. We'll lay the foundational plumbing that makes a multi-page experience possible in React.
From Server-Side to Client-Side Routing
With your background in Laravel, you're already familiar with routing. In a traditional multi-page application (MPA) like those often built with Laravel, clicking a link sends a request to the server. The server looks up the URL in its route file (web.php), runs the corresponding controller action, renders a new HTML (or Blade) view, and sends the complete page back to the browser. This process involves a full page refresh.
React applications, however, are typically Single-Page Applications (SPAs). The goal is to create a faster, more fluid user experience, similar to a desktop or mobile app. This is where client-side routing comes in.
When a user clicks a link in a React SPA managed by React Router:
- React Router intercepts the navigation event.
- It prevents the browser's default behavior of making a server request.
- It updates the URL in the browser's address bar using the modern History API.
- It then swaps the components displayed on the screen to match the new URL.
The result? The "page" changes instantly without a full refresh. The entire application shell (like headers, footers, and sidebars) can remain in place while only the main content area is updated.
Setting Up React Router
Getting started with React Router involves two main steps: installing the library and configuring it to wrap your application.
First, you need to add it to your project. Open your terminal in your project's root directory and run the following command:
npm install react-router-dom
This installs the specific version of React Router designed for web applications (as opposed to React Native).
Next, we need to make the routing functionality available to our entire component tree. We do this using the BrowserRouter component.
Learn React Router v6 In 45 Minutes
This video from Web Dev Simplified clearly demonstrates how to install React Router and wrap your application with BrowserRouter. This is the essential configuration step.
Watch from 00:40 to 01:53. Pay close attention to: The npm install command. How BrowserRouter is imported from react-router-dom. Where it is placed in main.jsx (or index.js) to wrap the <App /> component.
How BrowserRouter Works
As the video explains, the BrowserRouter component is the engine that powers routing in your app. By wrapping your top-level <App /> component, it uses React's Context API to provide routing information to every component nested within it. This is what allows any component in your app—no matter how deeply nested—to be aware of the current URL and to trigger navigation changes.
Here’s how your src/main.jsx file should look after this change:
// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom'; // 1. Import BrowserRouter
import App from './App';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
{/* 2. Wrap your App component */}
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
With this simple change, you have successfully configured React Router. Your application is now equipped to handle client-side navigation.
Verifying the Configuration
While we've installed and configured the router, how do we know it's working? We can define a single, simple route to prove that the router is now in control of rendering components based on the URL.
To do this, we use two new components from React Router:
<Routes>: A container component that holds all your individual route definitions. It looks at the current URL and finds the first matching child route to render.<Route>: Defines a single mapping between a URL path and a component. It takes two key props:pathandelement.
Let's modify your App.jsx to render a HomePage component when the URL is at the root (/).
First, create a simple HomePage component. You can make a new folder src/pages to keep your page-level components organized.
// src/pages/HomePage.jsx
import React from 'react';
const HomePage = () => {
return (
<div>
<h1>Welcome to the Home Page</h1>
<p>Our router is working!</p>
</div>
);
};
export default HomePage;
Now, update App.jsx to use the <Routes> and <Route> components.
// src/App.jsx
import React from 'react';
import { Routes, Route } from 'react-router-dom'; // 1. Import Routes and Route
import HomePage from './pages/HomePage';
function App() {
return (
<div>
{/* Any shared layout elements like a header can go here */}
<main>
{/* 2. Define your routes inside the Routes component */}
<Routes>
<Route path="/" element={<HomePage />} />
</Routes>
</main>
{/* Any shared layout elements like a footer can go here */}
</div>
);
}
export default App;
Now, when you run your app and navigate to the root URL, you should see the content from your HomePage component. You have successfully installed and configured React Router, and it is now actively managing which components to display.
Conclusion
In this lesson, you took the first and most critical step toward building a multi-page React application.
Key Takeaways:
- Client-side routing (used by React Router) creates a faster user experience by updating the UI without full page reloads, a key difference from the server-side routing you've used in Laravel.
- The
react-router-dompackage is the standard library for routing in web-based React apps. - Configuration is straightforward: you wrap your entire application with the
<BrowserRouter>component in your main entry file (main.jsx). - The
<BrowserRouter>component provides routing capabilities to your entire app. You can then use<Routes>and<Route>to map URL paths to specific components.
Preview of the Next Lesson:
You've laid the foundation. Now that the router is configured, the next step is to flesh out your application's structure. In the next lesson, we will focus on defining multiple application routes, mapping various URL paths to different page components, which will be the blueprint for our e-commerce site.