#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Routing and Navigation

Nested Routes

Nested routes let one route render inside another, so a parent page keeps its layout, such as a sidebar or tabs, while the inner part changes with the URL. In React Router, you nest <Route> elements and mark where the child should appear with the <Outlet /> component. You use them for dashboards, account settings, product sections with tabs, and any page that has sub-pages.

How the URL maps to nested layouts

Think of the URL /dashboard/orders as two layers. The /dashboard part picks a layout with a sidebar. The orders part picks what fills the main area. Nesting the routes mirrors that structure in code, and only the inner part re-renders when the user moves between sections.

import { BrowserRouter, Routes, Route, Outlet, NavLink } from 'react-router-dom';

function DashboardLayout() {
  return (
    <div className="dashboard">
      <nav>
        <NavLink to="" end>Overview</NavLink>
        <NavLink to="orders">Orders</NavLink>
        <NavLink to="profile">Profile</NavLink>
      </nav>
      <main>
        <Outlet />
      </main>
    </div>
  );
}

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/dashboard" element={<DashboardLayout />}>
          <Route index element={<Overview />} />
          <Route path="orders" element={<Orders />} />
          <Route path="orders/:orderId" element={<OrderDetail />} />
          <Route path="profile" element={<Profile />} />
        </Route>
        <Route path="*" element={<NotFound />} />
      </Routes>
    </BrowserRouter>
  );
}

The parts of a nested setup

  • Parent route: /dashboard renders DashboardLayout.
  • Child routes: written inside the parent, with paths relative to it. orders means /dashboard/orders.
  • Index route: <Route index> renders when the URL is exactly the parent path, so /dashboard shows Overview instead of an empty area.
  • Outlet: the placeholder in the parent's JSX where the matched child appears.

Relative links and dynamic children

Links inside the layout can be relative too. <NavLink to="orders"> inside DashboardLayout points to /dashboard/orders, so if you later move the dashboard to another path, these links still work. NavLink adds an active class to the current link; the end prop on the overview link stops it from staying active on every child page.

Child routes can have parameters, like orders/:orderId. Inside OrderDetail, useParams() returns { orderId: '...' }, exactly as with the dynamic routes from the previous lesson.

The missing Outlet problem

The most common mistake is defining child routes correctly but forgetting <Outlet /> in the parent component. The URL changes and the parent renders, but the child never appears, and there is no error message. If a nested page seems to do nothing, check the parent for an outlet first.

A second mistake is starting child paths with a slash. path="/orders" inside the dashboard route is treated as absolute and conflicts with the nesting, so leave the slash off child paths.

Sharing data with children

A layout can pass values to whatever renders in its outlet with <Outlet context={{ user }} />. Children read it with useOutletContext(). This avoids repeating the same fetch in each child page when the layout already has the data.

Nesting can go more than one level deep. An Orders page could have its own child routes for "pending" and "delivered" tabs, with its own <Outlet />. Each level only needs to know about its direct children, which keeps large route trees readable.

Nested routes also work as plain grouping without a visible layout. A parent route with no element simply renders its matching child, which is a tidy way to collect related paths under one prefix, or to wrap them in a component that checks whether the user is logged in.

Summary: nest <Route> elements, keep child paths relative, add an index route for the default view, and place an <Outlet /> where children should render.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course