Routing is a fundamental part of building web applications. In React, React Router DOM is the most popular library for handling client-side routing. With the release of v6, React Router introduced a more intuitive API, making it easier to manage nested routes, navigation, and dynamic routing. react router dom v6 example.
This guide will walk you through React Router DOM v6 examples, covering everything from basic routing to advanced use cases. Whether you’re a beginner or a professional developer, this blog will help you understand routing in React and implement it efficiently in your applications.
What is React Router DOM v6?
React Router DOM is a library that allows developers to handle dynamic navigation and routing in React applications. Version 6 brings simplified APIs, better route nesting, and improved performance, making it easier to manage routes and navigation.
Key improvements in v6 include:
- Simplified
<Routes>and<Route>components - Nested routes with relative paths
- Automatic route ranking for matching
- Enhanced navigation with
useNavigatehook
Why Use React Router DOM v6?
- Dynamic Routing: Easily navigate between pages without full reloads.
- Nested Routes: Build complex layouts with child routes.
- Better Performance: Faster route matching and rendering.
- Cleaner Code: Simplified APIs reduce boilerplate.
- SEO-Friendly: Works well with server-side rendering and SPA SEO practices.
Basic React Router DOM v6 Example
To get started, you need to install React Router DOM: react router dom v6 example.
npm install react-router-dom@6
Setting Up Basic Routes
import React from 'react';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
function Home() {
return <h2>Home Page</h2>;
}
function About() {
return <h2>About Page</h2>;
}
function App() {
return (
<Router>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
}
export default App;
<Routes>replaces<Switch>from v5.elementreplacescomponentfor route rendering.Linkprovides navigation without page reload.
Dynamic Routes Example
React Router v6 makes it easy to handle dynamic route parameters.
function User({ params }) {
return <h2>User ID: {params.id}</h2>;
}
function App() {
return (
<Router>
<nav>
<Link to="/user/1">User 1</Link>
<Link to="/user/2">User 2</Link>
</nav>
<Routes>
<Route path="/user/:id" element={<User />} />
</Routes>
</Router>
);
}
:idis a dynamic segment.- Use
useParams()hook to access route parameters:
import { useParams } from 'react-router-dom';
function User() {
const { id } = useParams();
return <h2>User ID: {id}</h2>;
}
Nested Routes in React Router DOM v6
Nested routes allow you to create layouts with child routes. react router dom v6 example.
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
<Routes>
<Route path="profile" element={<Profile />} />
<Route path="settings" element={<Settings />} />
</Routes>
</div>
);
}
function App() {
return (
<Router>
<Routes>
<Route path="dashboard/*" element={<Dashboard />} />
</Routes>
</Router>
);
}
- Nested routes are relative to their parent path.
- The
*in the path ensures nested routes are matched.
Using useNavigate for Programmatic Navigation
Instead of Link, you can navigate programmatically:
import { useNavigate } from 'react-router-dom';
function Home() {
const navigate = useNavigate();
const goToAbout = () => navigate('/about');
return <button onClick={goToAbout}>Go to About</button>;
}
useNavigate()replacesuseHistory()from v5.- You can navigate programmatically after actions like form submission.
Handling 404 Pages
You can create a catch-all route for unmatched URLs:
function NotFound() {
return <h2>404 - Page Not Found</h2>;
}
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes>
*matches all unmatched routes.- Provides better UX for users navigating to invalid URLs.
Private Routes in React Router v6
To protect routes, you can create a PrivateRoute wrapper:
import { Navigate } from 'react-router-dom';
function PrivateRoute({ isAuthenticated, children }) {
return isAuthenticated ? children : <Navigate to="/login" />;
}
// Usage
<Routes>
<Route
path="/dashboard"
element={
<PrivateRoute isAuthenticated={userLoggedIn}>
<Dashboard />
</PrivateRoute>
}
/>
</Routes>
NavigatereplacesRedirectfrom v5.- Useful for authentication and role-based routes. react router dom v6 example.
Common Challenges in React Router DOM v6
- Nested Routes Confusion
- Ensure
*is used in parent paths. - Use relative paths for child routes.
- Ensure
- Migrating from v5
Switch→Routescomponent→elementRedirect→Navigate
- Dynamic Parameters
- Always use
useParams()to read route parameters. - Avoid manually parsing URLs.
- Always use
- Programmatic Navigation
- Replace
history.push()withuseNavigate().
- Replace
Best Practices for React Router DOM v6
- Use
Routesandelementconsistently. - Leverage nested routes for clean layouts.
- Always handle 404 routes for unmatched paths.
- Use
useParamsfor dynamic routing. - Encapsulate private routes for authentication.
- Memoize components to avoid unnecessary re-renders during navigation.
Advanced React Router DOM v6 Examples
1. Nested Layout Example
function Layout() {
return (
<div>
<Header />
<Routes>
<Route path="home" element={<Home />} />
<Route path="profile" element={<Profile />} />
</Routes>
<Footer />
</div>
);
}
- Layout components like Header/Footer stay consistent.
- Only content in
<Routes>changes dynamically.
2. Lazy Loading Routes
import { lazy, Suspense } from 'react';
const Home = lazy(() => import('./Home'));
const About = lazy(() => import('./About'));
<Routes>
<Route path="/" element={<Suspense fallback={<p>Loading...</p>}><Home /></Suspense>} />
<Route path="/about" element={<Suspense fallback={<p>Loading...</p>}><About /></Suspense>} />
</Routes>
- Improves performance for large apps.
- Components are loaded only when needed.
3. Route Guards Example
function AdminRoute({ isAdmin, children }) {
return isAdmin ? children : <Navigate to="/not-authorized" />;
}
- Protect sensitive routes easily with role-based checks.
FAQs About React Router DOM v6
Q1. What is the difference between React Router v5 and v6?
- v6 uses
Routesinstead ofSwitch,elementinstead ofcomponent, andNavigateinstead ofRedirect.
Q2. How do I create nested routes in React Router v6?
- Use
<Routes>inside a parent component and define relative paths for children.
Q3. Can I navigate programmatically in v6?
- Yes, using the
useNavigate()hook.
Q4. How do I handle dynamic route parameters?
- Use
:paramin the route path and access it withuseParams().
Q5. How do I implement private routes in v6?
- Create a wrapper component that checks authentication and returns
<Navigate>if unauthorized.
Conclusion
React Router DOM v6 has simplified routing in React applications, making it easier to manage dynamic routes, nested routes, and programmatic navigation. By following best practices and using modern hooks like useNavigate and useParams, developers can build robust, scalable, and user-friendly applications.
Mastering React Router v6 is essential for modern React developers. With these examples, you can confidently implement routing in your projects, whether it’s for simple pages, complex nested layouts, or dynamic user-driven content.
![React Router DOM v6 Example: Master Routing in Modern React Apps [2026]](https://switzerlandtourpackage.com/wp-content/uploads/2026/01/david-pupaza-heNwUmEtZzo-unsplash-1.jpg)





Leave a Reply