π§ React Nested Routes & Layouts β Build Scalable Route Structures (2025 Guide)
π§² Introduction β Why Nested Routes & Layouts Matter
As React.js applications grow, organizing pages into nested routes and shared layouts becomes essential. With React Router v6+, it’s easier than ever to create structured, maintainable routing systems using nested <Route>
components and the powerful <Outlet />
component for rendering child routes.
π― In this guide, youβll learn:
- How to define nested routes in React Router
- How to use layouts with
<Outlet />
- Best practices for organizing multi-level navigation
- Real-world use cases like dashboards and profile pages
π§± 1. What Are Nested Routes?
Nested routes allow you to create a parent-child route structure, where child routes are rendered inside the parent component without reloading or duplicating layout code.
β Example:
/dashboard
β renders Dashboard layout/dashboard/stats
β renders inside Dashboard/dashboard/settings
β same layout, different content
βοΈ 2. Setup React Router with Nested Routes
β
Wrap App with <BrowserRouter>
:
import { BrowserRouter } from 'react-router-dom';
<BrowserRouter>
<App />
</BrowserRouter>
β Define Routes with Nesting:
import { Routes, Route } from 'react-router-dom';
import Dashboard from './pages/Dashboard';
import Stats from './pages/Stats';
import Settings from './pages/Settings';
function App() {
return (
<Routes>
<Route path="/dashboard" element={<Dashboard />}>
<Route path="stats" element={<Stats />} />
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
);
}
β
Stats
and Settings
are nested under /dashboard
π¦ 3. Using <Outlet />
for Nested Route Rendering
Inside the parent route component, use <Outlet />
to render its child routes.
β
Example β Dashboard.jsx
:
import { Outlet, Link } from 'react-router-dom';
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<nav>
<Link to="stats">Stats</Link>
<Link to="settings">Settings</Link>
</nav>
<hr />
<Outlet />
</div>
);
}
π <Outlet />
is a placeholder for child routes
π§ 4. Deeply Nested Routes
You can nest routes as deeply as needed:
<Route path="/dashboard" element={<Dashboard />}>
<Route path="stats" element={<Stats />}>
<Route path="details" element={<StatsDetails />} />
</Route>
</Route>
Inside Stats.jsx
, use another <Outlet />
to render StatsDetails
.
π 5. Layout Components for Shared UI
Use layout components to wrap multiple pages with shared headers, footers, or sidebars.
β Example Layout:
function MainLayout() {
return (
<div>
<Header />
<main><Outlet /></main>
<Footer />
</div>
);
}
β Apply Layout to Routes:
<Route path="/" element={<MainLayout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
</Route>
β Keeps your app DRY and UI consistent
π 6. Nested Navigation with <Link>
or <NavLink>
<Link to="settings">Settings</Link> // Relative path
<Link to="/dashboard/stats">Stats</Link> // Absolute path
β
Use relative paths inside nested routes
β
Use <NavLink>
for active link styling
π 7. Nested Routes Example Structure
src/
βββ App.jsx
βββ pages/
β βββ Dashboard.jsx
β βββ Stats.jsx
β βββ Settings.jsx
βββ layouts/
β βββ MainLayout.jsx
β
Keep components modular
β
Use /pages
for routes, /layouts
for shared UI
π Best Practices
β
Use <Outlet />
for all parent route components
β
Group routes by feature or layout
β
Use layout components for shared UI (header, sidebar)
β
Keep URLs RESTful and meaningful
β
Add a wildcard route (*
) for handling 404s
π Summary β Recap & Next Steps
React Routerβs nested routes and layout support allow developers to build modular, structured, and reusable routing systems. This enables clean code and consistent UIs across large applications.
π Key Takeaways:
- Use nested
<Route>
components to build hierarchy - Render child routes using
<Outlet />
- Organize shared UI with layout components
- Prefer relative
<Link>
s inside nested components - Scale cleanly with modular folders and route configs
βοΈ Real-World Relevance:
Used in dashboards, portals, admin systems, and apps with multiple views β nested routes simplify route and layout logic in complex React apps like Notion, Stripe Dashboard, and GitHub.
β FAQ Section
β What does <Outlet />
do in React Router?
β
It renders the matched child route inside the parent componentβs layout.
β Can I nest routes infinitely in React?
β
Yes, but keep your structure manageable and modular.
β How do I create layouts with navigation?
β
Wrap nested routes in a layout component with navigation and use <Outlet />
to render content.
β What’s the difference between relative and absolute links?
β
Relative links (to="settings"
) are based on the current path. Absolute links (to="/dashboard/settings"
) start from the root.
β Can I use multiple <Outlet />
s in one layout?
β
Not directly β one <Outlet />
per nesting level. You can structure additional routes or fragments inside.
Share Now :