Introduction
Routing is an essential part of any single-page application (SPA) built with React. React Router is the de facto library for handling client-side routing in React applications. While simple routes are easy to implement, modern applications often require nested routes, where multiple levels of routes exist within a parent route.
Nested routes allow developers to create complex user interfaces with reusable layouts, multiple sections, or tabs within a single page. This post will cover:
- Concept of nested routes
- Using
Outletfor rendering child routes - Example: Dashboard with nested tabs or sections
- Benefits of nested routes for complex UIs
1. Concept of Nested Routes
Nested routes are routes that exist inside other routes. They allow you to render child components inside a parent component based on the URL path.
Example:
/dashboard→ Parent route/dashboard/profile→ Child route/dashboard/settings→ Child route
This structure allows the Dashboard component to act as a layout, while child routes like Profile and Settings are rendered within it.
Key Concepts
- Parent Route: Contains the layout or wrapper for child routes.
- Child Route: Nested inside the parent and rendered in the parent’s layout.
- Outlet: Placeholder in the parent component where the child route is rendered.
2. Using Outlet for Rendering Child Routes
In React Router v6+, the Outlet component is used as a placeholder in the parent component for child routes.
Example: Basic Outlet Usage
import React from "react";
import { BrowserRouter as Router, Routes, Route, Link, Outlet } from "react-router-dom";
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<nav>
<Link to="profile">Profile</Link> | <Link to="settings">Settings</Link>
</nav>
<Outlet />
</div>
);
}
function Profile() {
return <h2>User Profile</h2>;
}
function Settings() {
return <h2>Settings Page</h2>;
}
function App() {
return (
<Router>
<Routes>
<Route path="dashboard" element={<Dashboard />}>
<Route path="profile" element={<Profile />} />
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
</Router>
);
}
export default App;
Explanation:
Dashboardis the parent route.ProfileandSettingsare nested child routes.Outletrenders whichever child route matches the current URL.- Navigation links use relative paths (
profile,settings).
3. Example: Dashboard with Nested Tabs or Sections
Nested routes are ideal for applications with tabbed navigation or multiple sections inside a page.
Step 1: Creating Parent Dashboard Component
import React from "react";
import { Link, Outlet } from "react-router-dom";
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<nav>
<Link to="overview">Overview</Link> |
<Link to="analytics">Analytics</Link> |
<Link to="settings">Settings</Link>
</nav>
<div style={{ border: "1px solid #ccc", padding: "20px", marginTop: "10px" }}>
<Outlet />
</div>
</div>
);
}
export default Dashboard;
Outletis where nested routes will be displayed.- Navigation links are relative, automatically resolving to
/dashboard/overview, etc.
Step 2: Creating Child Components
import React from "react";
export function Overview() {
return <h2>Dashboard Overview</h2>;
}
export function Analytics() {
return <h2>Analytics Section</h2>;
}
export function Settings() {
return <h2>Dashboard Settings</h2>;
}
Step 3: Defining Routes
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Dashboard from "./Dashboard";
import { Overview, Analytics, Settings } from "./DashboardSections";
function App() {
return (
<Router>
<Routes>
<Route path="dashboard" element={<Dashboard />}>
<Route path="overview" element={<Overview />} />
<Route path="analytics" element={<Analytics />} />
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
</Router>
);
}
export default App;
Key Points:
- Nested routes allow each section of the dashboard to have its own component.
- The parent layout remains intact while child content changes dynamically.
- Navigation is smooth, and URLs reflect the current section.
Step 4: Redirect to Default Child Route
Often, you want to show a default child route when a parent route is accessed.
import { Navigate } from "react-router-dom";
<Route path="dashboard" element={<Dashboard />}>
<Route index element={<Navigate to="overview" />} />
<Route path="overview" element={<Overview />} />
<Route path="analytics" element={<Analytics />} />
<Route path="settings" element={<Settings />} />
</Route>
indexroute handles default navigation.- Users visiting
/dashboardare automatically redirected to/dashboard/overview.
4. Benefits of Nested Routes for Complex UIs
4.1 Reusable Layouts
- Nested routes allow parent components to act as layouts for multiple pages.
- Example: Dashboard, Admin panel, or e-commerce category pages.
4.2 Cleaner URL Structure
- Nested routes create intuitive URLs that reflect the app’s hierarchy.
- Example:
/dashboard/settings/privacyclearly shows a settings section.
4.3 Reduced Code Duplication
- Parent components provide common UI elements (navbars, sidebars).
- Child components only need to handle their specific content.
4.4 Easy Navigation and State Management
- State related to the layout (like sidebar collapse) can reside in the parent.
- Child components do not need to manage layout state individually.
4.5 Dynamic Rendering
- Components can be rendered conditionally based on nested routes.
- Works well with tabs, wizards, and multi-step forms.
5. Advanced Example: Multi-Level Nested Routes
Nested routes can go multiple levels deep:
<Route path="dashboard" element={<Dashboard />}>
<Route path="analytics" element={<AnalyticsLayout />}>
<Route path="sales" element={<Sales />} />
<Route path="marketing" element={<Marketing />} />
</Route>
<Route path="settings" element={<Settings />} />
</Route>
AnalyticsLayoutcan have its ownOutletfor sub-routes likesalesandmarketing.- Multi-level nesting allows complex UIs to be structured cleanly.
6. Tips and Best Practices
- Use Outlet for Parent Layouts – Always use
Outletto render child routes dynamically. - Keep Routes Organized – Structure route definitions logically in separate files if necessary.
- Use Relative Paths in Links – Simplifies navigation inside nested routes.
- Leverage Index Routes – Provide default content when a parent route is accessed.
- Avoid Excessive Nesting – Too many levels can make routes difficult to maintain.
- Combine with React State or Context – Nested routes work seamlessly with global state management.
7. Real-World Use Cases
- Admin Dashboards – Separate sections like users, analytics, reports.
- E-commerce Applications – Nested routes for categories, products, and product details.
- Profile Management – Tabs for personal info, security settings, and preferences.
- Multi-Step Forms – Each step can be a nested route.
Leave a Reply