Member-only story
Routing in React: Using React Router to Navigate Between Pages
4 min readJan 3, 2025
Routing is a fundamental aspect of web applications, allowing users to navigate between different views or pages. React provides an excellent library called React Router to manage routing efficiently in single-page applications (SPAs).
What is React Router?
React Router is a popular library for handling navigation in React applications. It enables dynamic routing, allowing your app to switch between components or pages without a full page reload. React Router uses the browser’s History API to maintain a seamless user experience.
Key Components of React Router
- BrowserRouter:
- Acts as the parent container for routing.
- Synchronizes the UI with the browser’s URL.
- Wraps your application and provides routing functionality.
import { BrowserRouter } from "react-router-dom";
<BrowserRouter>
<App />
</BrowserRouter>
- Routes:
- Defines different paths and maps them to specific components.
- Replaces the older<Switch>
component in React Router v6.
import { Routes, Route } from "react-router-dom";
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
- Route:
- Represents a single…