π§ React Routing β Create Multi-Page Navigation with React Router (2025 Guide)
π§² Introduction β Why Routing Matters in React
React.js is designed for building Single Page Applications (SPAs), where navigation happens without full-page reloads. But for SPAs to mimic traditional multi-page apps, we need client-side routing β and thatβs where React Router comes in.
π― In this guide, youβll learn:
- How to set up React Router in a project
- Create multiple routes and pages
- Use dynamic routes, nested routes, redirects, and navigation
- Best practices for organizing route-based components
π Topics Covered
πΉ Topic | π Description |
---|---|
π§ React Route Parameters (useParams ) | Capture and use dynamic values from the URL in components |
π§ React Nested Routes & Layouts | Create route hierarchies and shared layout structures |
π§ React Navigation with useNavigate & Link | Enable navigation between pages programmatically or via links |
π§ React Redirects & 404 Error Handling | Handle page redirects and create custom 404 not found pages |
π§° 1. Installing React Router
React Router is the most popular routing library in the React ecosystem.
β Installation:
npm install react-router-dom
For React Router v6+ (2025), this installs all necessary hooks and components for browser-based routing.
ποΈ 2. Basic Setup β BrowserRouter
Wrap your app in BrowserRouter
to enable client-side navigation.
import { BrowserRouter } from 'react-router-dom';
import App from './App';
<BrowserRouter>
<App />
</BrowserRouter>
Then use the Routes
and Route
components to define paths.
π 3. Defining Routes
β Example:
import { Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
function App() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
);
}
π Each <Route>
defines a path and the component it should render.
π§ 4. Navigation with <Link>
and <NavLink>
React Router provides <Link>
for navigation without page reloads.
import { Link } from 'react-router-dom';
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<NavLink>
adds active class:
<NavLink to="/about" className={({ isActive }) => isActive ? 'active' : ''}>
About
</NavLink>
β Ideal for styling active tabs or sidebar menus
π 5. Dynamic Route Parameters
Pass URL parameters with :id
, and extract them using useParams
.
β Define route:
<Route path="/user/:id" element={<UserProfile />} />
β Use in component:
import { useParams } from 'react-router-dom';
function UserProfile() {
const { id } = useParams();
return <h2>User ID: {id}</h2>;
}
π§© 6. Nested Routes (Route Outlets)
Use nested <Route>
blocks and <Outlet />
to structure hierarchies.
<Route path="/dashboard" element={<Dashboard />}>
<Route path="stats" element={<Stats />} />
<Route path="settings" element={<Settings />} />
</Route>
β
Inside Dashboard.jsx
:
import { Outlet } from 'react-router-dom';
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
<Outlet />
</div>
);
}
β Allows layout reuse and structured navigation
π 7. Redirects and Navigate
Use useNavigate()
to redirect programmatically.
import { useNavigate } from 'react-router-dom';
function Login() {
const navigate = useNavigate();
const handleLogin = () => {
// after auth logic
navigate('/dashboard');
};
return <button onClick={handleLogin}>Login</button>;
}
Or use <Navigate />
for declarative redirects:
<Route path="/home" element={<Navigate to="/" />} />
π 8. Protecting Routes (Private Routing)
Use conditional rendering to protect private pages:
<Route path="/dashboard" element={isLoggedIn ? <Dashboard /> : <Navigate to="/login" />} />
β Combine with context or auth state to restrict access
π§ 9. React Router Hooks Summary
Hook | Purpose |
---|---|
useParams() | Access route parameters (e.g., :id ) |
useNavigate() | Programmatic navigation |
useLocation() | Get current URL location info |
useRoutes() | Define routes with config objects |
π Best Practices
β
Use a layout component with <Outlet />
for nested routes
β
Place <Routes>
inside a centralized router component
β
Avoid reloads β use <Link>
, not <a>
β
Use lazy loading for routes in large apps
β
Keep route definitions organized in one file (route config)
π Summary β Recap & Next Steps
React Router enables powerful and efficient client-side navigation in React applications. With support for dynamic routes, nested layouts, and protected views, it provides all the tools needed for building full-featured SPAs.
π Key Takeaways:
- Use
<Routes>
and<Route>
to define page paths - Use
<Link>
and<NavLink>
for internal navigation - Access parameters with
useParams()
- Protect routes using conditional logic and
<Navigate />
- Use
<Outlet />
for nesting and shared layouts
βοΈ Real-World Relevance:
React Router powers navigation for apps like Vercel Dashboard, Netlify CMS, and countless admin panels and user-facing dashboards.
β FAQ Section
β What is React Router?
β
Itβs a library for routing in React apps that allows navigation between views without full-page reloads.
β Whatβs the difference between <Link>
and <a>
?
β
<Link>
updates the browser URL without reloading the page. <a>
triggers a full reload.
β How do I redirect after login in React?
β
Use useNavigate()
:
const navigate = useNavigate();
navigate('/dashboard');
β How do I handle 404 routes?
β
Add a wildcard route:
<Route path="*" element={<NotFound />} />
β Can I use React Router with Next.js?
β No. Next.js has its own built-in routing system based on file structure.
Share Now :