L2: Setting Up the Router
Create the Router component and configure React Router in your application
Now that React Router is installed, let's set up the routing infrastructure for your application! We'll create a Router component and wrap our app with BrowserRouter.
What You'll Learn
- How to set up
BrowserRouterin your app - Creating a centralized Router component
- Understanding the routing hierarchy
- Preparing for route definitions
Understanding BrowserRouter
BrowserRouter is the foundation of React Router. It must wrap your entire application to enable routing.
How It Works
<BrowserRouter>
{/*
Everything inside has access to routing features:
- useNavigate hook
- useParams hook
- Link component
- Route definitions
*/}
<App />
</BrowserRouter>Router Context
BrowserRouter creates a React Context that provides routing data to all components below it. This is why it must be at the root of your app!
Two Approach Options
You can set up routing in two ways:
Create a separate Router component (Recommended)
Pros:
- ✅ Clean separation of concerns
- ✅ Easy to modify routes
- ✅ Testable in isolation
- ✅ Follows single responsibility principle
Structure:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
export default function Router() {
return (
<BrowserRouter>
<Routes>
{/* Route definitions here */}
</Routes>
</BrowserRouter>
);
}import Router from './components/Router';
function App() {
return <Router />;
}We'll use this approach!
Put routing directly in App.jsx
Pros:
- ✅ Fewer files
- ✅ Simpler for small apps
Cons:
- ❌ App.jsx becomes bloated
- ❌ Harder to maintain
Structure:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
{/* Route definitions here */}
</Routes>
</BrowserRouter>
);
}This works, but isn't ideal for larger apps.
Step 1: Create the Router Component
Build the Basic Structure
Add this code to Router.jsx:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
export default function Router() {
return (
<BrowserRouter>
<Routes>
{/* Routes will be defined here in the next lessons */}
</Routes>
</BrowserRouter>
);
}What's happening:
- Import statements - Getting routing components from React Router
- BrowserRouter - Enables routing for the entire app
- Routes - Container for all route definitions
- Route - Individual routes will go inside (next lesson)
Step 2: Update App.jsx
Now let's use our new Router component in App.jsx.
Current App.jsx
Your App.jsx currently looks like this:
import HomePage from './pages/HomePage';
function App() {
return <HomePage />;
}
export default App;It directly renders HomePage. We'll change this!
Update with Router
Replace the content with:
//import Router from './components/Router';
//import HomePage from './pages/HomePage';
function App() {
//return <HomePage />;
//return <Router />;
}
export default App;What changed:
- Removed direct
HomePageimport - Imported
Routercomponent - Rendering
Routerinstead ofHomePage
Complete Updated App.jsx
Your App.jsx should now look like this:
import Router from './components/Router';
function App() {
return <Router />;
}
export default App;Simple and clean! All routing logic is now contained in the Router component.
Step 3: Verify the Setup
Let's make sure everything is working:
Check Your Browser
Open your browser and go to http://localhost:5173/
You should see: A blank page (this is expected!)
Why blank? We haven't defined any routes yet. The <Routes> component is empty, so nothing renders.
Understanding the Routing Hierarchy
Your application now has this component structure:
App
└─ Router
└─ BrowserRouter (provides routing context)
└─ Routes (matches URL to routes)
└─ Route (individual routes)
└─ Component (HomePage, DetailsPage, etc.)Context Flow
Routing context flows down from BrowserRouter:
BrowserRouter (creates context)
↓
Routes (consumes context, matches URL)
↓
Your Components (can use routing hooks)File Structure Check
Your project should now have this structure:
Alternative Routers
While we're using BrowserRouter, React Router offers other router types:
Common Questions
Router Setup Complete!
Your app is now routing-ready! The foundation is in place, and in the next lesson, we'll add our first route to bring back the HomePage.
Quick Recap
What we accomplished:
- ✅ Created a
Routercomponent - ✅ Set up
BrowserRouterandRoutes - ✅ Updated
App.jsxto use Router - ✅ Understood the routing hierarchy
- ✅ Verified the setup (blank page expected)
Key concepts:
- BrowserRouter - Provides routing context to entire app
- Routes - Container for route definitions
- Route - Individual URL → Component mapping
- Component hierarchy - Router context flows down
What's Next?
In Lesson 3, we'll define our first route and get the HomePage showing again. We'll learn about route paths, the element prop, and index routes!