🧭 React Routing
Estimated reading: 4 minutes 32 views

🧭 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧭 React Nested Routes & Layouts

Or Copy Link

CONTENTS
Scroll to Top