Setting Up BrowserRouter
In this lesson, we will learn how to configure BrowserRouter, wrap our React application with it, and prepare the application for URL-based navigation.
1. What Is BrowserRouter?
BrowserRouter is a component provided by React Router that uses the browser's URL and history to manage client-side navigation in a React application.
It provides routing context to the components rendered inside it, allowing them to use routing features such as Routes, Route, Link, and navigation hooks.
Remember: BrowserRouter provides the routing environment. We define individual URL paths using Route components inside Routes.
2. Why Do We Need BrowserRouter?
- To enable browser-based routing.
- To keep track of the current URL.
- To support client-side navigation between routes.
- To provide routing context to child components.
- To work with browser history during navigation.
3. Basic BrowserRouter Structure
BrowserRouter generally wraps the part of the application that needs routing. Routes and Route are placed inside it to define the pages that users can access.
4. Import BrowserRouter
First, import BrowserRouter from react-router-dom.
import { BrowserRouter } from 'react-router-dom';
If the package is not installed, run the following command from the React project directory.
npm install react-router-dom
5. Set Up BrowserRouter in main.jsx
In a Vite React project, main.jsx is commonly the entry point of the application. You can wrap the App component with BrowserRouter here.
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App.jsx';
ReactDOM.createRoot(
document.getElementById('root')
).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
Explanation
- BrowserRouter: Enables browser-based routing.
- App: Contains the main application components.
- ReactDOM.createRoot: Mounts the React application.
- React.StrictMode: Helps identify potential problems during development.
Important: In this setup, do not wrap App in another BrowserRouter inside App.jsx. Use one BrowserRouter for this routing tree.
6. Define Routes in App.jsx
After setting up BrowserRouter, define the application's routes using Routes and Route.
import { Routes, Route } from 'react-router-dom';
function Home() {
return <h2>Home Page</h2>;
}
function About() {
return <h2>About Page</h2>;
}
function App() {
return (
<div>
<h1>CIIT Training Institute</h1>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</div>
);
}
export default App;
Expected Output
When you open the root URL:
CIIT Training Institute
Home Page
When you open the /about URL:
CIIT Training Institute
About Page
These outputs assume the development server is running and the corresponding URL is opened in the browser.
7. BrowserRouter and Link
Once BrowserRouter is configured, components inside it can use Link to navigate between defined routes.
import { Link } from 'react-router-dom';
function Navigation() {
return (
<nav>
<Link to="/">Home</Link>
{' | '}
<Link to="/about">About</Link>
</nav>
);
}
export default Navigation;
Render Navigation inside the application where you want the links to appear. Both destination paths must have matching route definitions.
8. Common BrowserRouter Errors
Error 1: Link used outside a router
Components that use Link or routing hooks must be rendered inside the appropriate router provider.
Error 2: Multiple BrowserRouter wrappers
Avoid unnecessarily nesting BrowserRouter instances around the same routing tree.
Error 3: Route does not match
Verify that the browser URL matches the path configured in the Route component.
Error 4: Direct URL returns a server error
In a deployed single-page application, the web server may need to serve the application entry point for supported client-side routes. Configure the appropriate fallback for your hosting environment.
Practice Tasks
- Explain the purpose of BrowserRouter.
- Install React Router in a React project.
- Configure BrowserRouter in main.jsx.
- Create Home and About components.
- Define routes for / and /about.
- Create navigation links using Link.
- Test both routes in the browser.
- Identify and fix a routing error caused by a missing router provider.
Summary
BrowserRouter provides browser-based routing for a React application and makes routing context available to its child components. It can wrap the App component in main.jsx, while Routes and Route define the URL paths and their corresponding components. Link can then navigate between those routes. In the next lesson, we will study Routes and Route components in detail.