Introduction to React Router
In this lesson, we will understand React Router, why it is useful, its main components, and how it connects URL paths with React components.
1. What Is React Router?
React Router is a routing library for React applications. It allows developers to display different components based on the current URL and navigate between application views.
For example, a training institute website can display its Home page at /, Courses page at /courses, and Contact page at /contact.
Remember: React Router helps manage navigation in a React application by connecting URL paths to the appropriate components.
2. Why Do We Use React Router?
- To create navigation between different application views.
- To display components according to the current URL.
- To create multi-page experiences within a React application.
- To support dynamic URLs such as product or student details.
- To manage nested pages and not-found routes.
- To navigate programmatically after actions such as form submission.
3. How React Router Works
React Router checks the current URL and matches it with a configured route. When a route matches, its associated component is rendered.
4. Important React Router Components and Hooks
BrowserRouter
BrowserRouter provides routing functionality based on the browser's URL and history.
Routes
Routes groups the route definitions used by the application.
Route
Route defines which component should be displayed for a particular URL path.
Link
Link provides navigation between routes without requiring a normal full-page link navigation.
NavLink
NavLink is useful when navigation links need styling based on whether their route is currently active.
useNavigate
The useNavigate hook allows a component to navigate to another route in response to an event or application action.
useParams
The useParams hook reads dynamic parameters from the current matched route.
5. Example of Route Definitions
The following example demonstrates the basic structure of routes using React Router. We will configure BrowserRouter and install the required package in upcoming lessons.
import {
BrowserRouter,
Routes,
Route
} from 'react-router-dom';
function Home() {
return <h2>Home Page</h2>;
}
function About() {
return <h2>About Page</h2>;
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Expected Behavior
- Opening / displays the Home Page.
- Opening /about displays the About Page.
Note: This example assumes React Router is installed and the application is configured to support these routes.
6. React Router Navigation Example
React Router provides the Link component for moving between routes. Unlike a regular anchor navigation, Link performs client-side navigation within the configured router.
import { Link } from 'react-router-dom';
function Navigation() {
return (
<nav>
<Link to="/">Home</Link>
{' | '}
<Link to="/about">About</Link>
</nav>
);
}
Clicking Home navigates to the root route, while clicking About navigates to the /about route. The matching route must be configured in the application.
7. React Router Project Structure
A simple React application using React Router can organize page components and routing configuration separately.
src/
components/
Navigation.jsx
pages/
Home.jsx
About.jsx
Contact.jsx
App.jsx
main.jsx
This is an example structure. You can adjust it according to the size and requirements of your project.
Practice Tasks
- Define React Router in your own words.
- Write any five advantages of using React Router.
- Explain the purpose of BrowserRouter, Routes, and Route.
- Write a route definition for a Courses component.
- Create Link elements for Home, About, and Contact pages.
- Explain the difference between Link and NavLink.
- Describe how useNavigate and useParams are used.
Summary
React Router is a library that manages navigation and URL-based views in React applications. BrowserRouter provides browser routing, Routes groups route definitions, and Route connects URL paths with components. Link and NavLink support navigation, while useNavigate and useParams help with programmatic navigation and dynamic route parameters. In the next lesson, we will learn how to install React Router in a React project.