Sub topic 5
Nested routes, layouts, and Outlet
This chapter assumes you can read app/routes.ts, know how a route module exports a default component, and are comfortable with React components and props. Here you will see how routes nest, how a parent renders its children with Outlet, and how a layout route shares UI without adding a URL segment.
How nesting works
A route can have child routes. The parent's component renders first. It decides where the matched child appears by rendering <Outlet />. Without an Outlet, the child never shows up, which is the most common nesting mistake.
There are two ways to group routes in app/routes.ts:
route(path, file, children)adds a URL segment and nests children under it.layout(file, children)wraps children in a shared parent but adds no URL segment.
The index helper marks the default child. It renders in the parent's Outlet when the URL matches the parent exactly.
A layout route
This config wraps three contact pages in one layout. The URLs stay /contacts, /contacts/new, and /contacts/:contactId.
import {
type RouteConfig,
index,
route,
layout,
} from "@react-router/dev/routes";
export default [
index("routes/home.tsx"),
layout("routes/contacts/layout.tsx", [
route("contacts", "routes/contacts/list.tsx"),
route("contacts/new", "routes/contacts/new.tsx"),
route("contacts/:contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;The layout module renders the shared frame and an Outlet:
import { Outlet } from "react-router";
export default function ContactsLayout() {
return (
<div>
<header>
<h2>Contacts</h2>
</header>
<Outlet />
</div>
);
}When the user visits /contacts/new, React Router renders ContactsLayout, and the new.tsx component appears inside its Outlet. Moving between the three child pages keeps the layout mounted, so its state is preserved.
Nesting with a URL segment
If the parent should also contribute a path, pass the path to route and put children in the third argument. Child paths are relative to the parent.
import {
type RouteConfig,
index,
route,
} from "@react-router/dev/routes";
export default [
route("contacts", "routes/contacts/layout.tsx", [
index("routes/contacts/list.tsx"),
route("new", "routes/contacts/new.tsx"),
route(":contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;This produces the same URLs. The difference is structural. The parent now owns the /contacts segment, and list.tsx is its index child.
Why nesting matters for data
Each matched route runs its own loader. React Router calls the loaders of the parent and child in parallel, so a layout's data does not delay the child's data. On navigation, only routes whose data changed need to reload.
Choosing between `route` and `layout`
| Need | Use |
|---|---|
| Shared UI, no extra URL segment | `layout` |
| Shared UI and a shared path prefix | `route` with children |
| Default page at the parent's URL | `index` |
Exercise
A parent route file renders a header but the child page never appears when you visit its URL. What is the most likely cause, and how do you fix it?
Show answer
The parent component does not render <Outlet />. Import Outlet from "react-router" and render it where the child should appear.
Exercise
You want /settings and /profile to share one sidebar component, but you do not want a /account prefix in the URLs. Which helper do you use?
Show answer
Use layout("routes/account-layout.tsx", [...]) with route("settings", ...) and route("profile", ...) as its children. A layout adds no URL segment.