Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท Routes and Nested Layouts

Nested Routes and Layouts with Outlet

Learn nested routes, layout routes, index routes and the Outlet component in React Router v7 framework mode, and how nested UI loads data in parallel, with runnable stand-ins.

Nested routing is the idea that makes Remix feel different from older frameworks. A URL such as /dashboard/settings does not match one component. It matches a chain of routes: the root, the dashboard layout, and the settings page. Each one renders its piece of the screen, and each piece knows where its child goes.

Why bother? Because the pieces that do not change stay on the screen and keep their state. Going from /dashboard/settings to /dashboard/billing swaps only the inner part; the dashboard navigation, its scroll position and an open sidebar are untouched. Data loading follows the same structure, so only the changed routes load new data.

The Outlet component

A parent route renders its child with <Outlet />. Everything around the outlet is the shared layout.

TSX
// app/routes/dashboard/layout.tsx  (real React Router v7, read-only)
import { NavLink, Outlet } from "react-router";

export default function DashboardLayout() {
  return (
    <div className="grid grid-cols-[14rem_1fr] min-h-screen">
      <nav className="border-r p-4">
        <h2>Dashboard</h2>
        <NavLink to="/dashboard" end>Overview</NavLink>
        <NavLink to="/dashboard/settings">Settings</NavLink>
        <NavLink to="/dashboard/billing">Billing</NavLink>
      </nav>
      <main className="p-6">
        <Outlet />
      </main>
    </div>
  );
}

With the config from the previous lesson:

TypeScript
route("dashboard", "routes/dashboard/layout.tsx", [
  index("routes/dashboard/overview.tsx"),
  route("settings", "routes/dashboard/settings.tsx"),
  route("billing", "routes/dashboard/billing.tsx"),
]),

the URL /dashboard/settings renders root > DashboardLayout > Settings. The root also contains an <Outlet />, which is how the dashboard gets into the page.

Stand-in: nested rendering with outlets

The router builds the chain, then renders from the inside out: the deepest route first, its output passed up to the parent as the "outlet". This stand-in does exactly that with strings so you can see the nesting.

JS Stand-in: render a chain of routes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The overview route has index: true, so /dashboard renders it inside the layout, and /dashboard/settings swaps only the part inside <main>.

Index routes

An index route is the default child. Without it, /dashboard would show the layout and an empty <main>. You write it with index(file) in routes.ts, or _index in the file-name convention. An index route cannot have children and has no path of its own.

Layout routes without a URL

Sometimes pages share a wrapper but not a URL prefix, such as marketing pages /pricing and /contact sharing a header. The layout() helper adds the wrapper without adding a path segment. You can nest layouts: an AuthLayout for /login and /register and a separate AppLayout for signed-in pages, both at the top of the URL.

Nested UI, nested URL

root.tsx html, head, body
dashboard layout Sidebar and Outlet
settings Only this part swaps

Each layer renders an Outlet for the next one.

1 / 2

Sharing values with Outlet context

A layout can pass a value to whatever it renders with <Outlet context={...} />. Children read it with useOutletContext(). This is meant for small values that belong to the layout (a theme or the selected project), not for data loading, which has better tools.

TSX
// layout
<Outlet context={{ project }} />

// child
import { useOutletContext } from "react-router";
const { project } = useOutletContext<{ project: Project }>();

For data that comes from a loader, prefer useRouteLoaderData("routes/dashboard/layout"). It reads a parent's loader result without prop drilling and without a second request; you will use it in parent and child data.

Why nesting makes data loading faster

On a transition from /dashboard/settings to /dashboard/billing the router asks: which matched routes are new? The dashboard layout is still matched, so its loader does not run again. Only billing loads. When you arrive at a URL for the first time, every matched route's loader runs in parallel, not one after another. The next stand-in measures that difference.

JS Stand-in: parallel loaders versus waterfall

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The router does the second thing. You never write Promise.all for matched routes; you just write one loader per route and the framework runs them together.

Common mistakes

  • Forgetting <Outlet />: the child route matches but nothing renders.
  • Adding data fetching to the layout that only one child needs; put it in the child.
  • Expecting a layout's loader to rerun on child navigation. It does not unless revalidation is triggered (after an action, for example).
  • Using useOutletContext for server data that every page needs; a loader is better.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does <Outlet /> render?

    A parent renders its matched child where you place the Outlet.
  2. 2. Navigating from /dashboard/settings to /dashboard/billing. Which loaders run?

    Routes that stay matched keep their data. Only the new routes load (unless revalidation is triggered).
  3. 3. What is the job of an index route?

    Without an index route, the parent's Outlet would be empty at the parent's own URL.
  4. 4. How do matched loaders run on a first visit?

    All matched route loaders start together, which avoids request waterfalls.

Summary

  • A URL matches a chain of nested routes, each rendering an <Outlet /> for the next.
  • Index routes fill a layout at its own URL; layout() adds a wrapper with no URL segment.
  • Unchanged layouts keep their state and are not reloaded on child navigation.
  • Matched loaders run in parallel.
  • useOutletContext shares small layout values; loader data is read with route hooks.

Next: Dynamic and Splat Routes