Sub topic 5
Nested routes, layouts, and Outlet
This chapter assumes you can read React components and TypeScript, and that you have a framework-mode project with an app/routes.ts file. You should know how route and index map a URL to a route module. Here you will learn how routes nest, how a parent renders its child with Outlet, and how layout routes share UI without adding to the URL.
The nesting model
In framework mode, a URL can match several route modules at once. The matched routes form a chain from the root route down to the deepest match. Each route in the chain renders its own component. A parent shows its child at the place where it renders <Outlet />.
Each level of the chain also runs its own loader. Loaders for all matched routes start in parallel when a request arrives, so nesting does not slow data loading by making requests wait on each other.
Two ways to nest
You can nest routes in two ways in app/routes.ts. Both use the optional children array, which is the last argument to route and layout.
Parent routes with a URL segment
A parent route created with route adds its path to the URL. The children's paths are appended to it.
import { type RouteConfig, route, index } from "@react-router/dev/routes";
export default [
route("contacts", "routes/contacts/parent.tsx", [
index("routes/contacts/list.tsx"),
route(":contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;This config matches these URLs:
| URL | Matched routes (outer to inner) |
|---|---|
| `/contacts` | `parent.tsx`, `list.tsx` (index) |
| `/contacts/42` | `parent.tsx`, `detail.tsx` |
An index route is a child that renders when the URL matches the parent exactly. It has no path of its own. Without it, /contacts would render the parent with an empty outlet.
Layout routes without a URL segment
A layout route created with layout wraps its children in shared UI but adds nothing to the URL. Use it when several pages need the same sidebar or header but their paths do not share a prefix.
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;Here / renders only home.tsx inside the root. The three /contacts URLs render inside layout.tsx. The word "layout" never appears in a URL.
A common gotcha: route("contacts", ...) and route("contacts/new", ...) as siblings do not nest. Matching is by path, and each URL matches exactly one of them. Nesting happens only when you pass a children array.
Rendering children with Outlet
A parent route module renders its matched child with Outlet, which React Router exports from react-router.
import { NavLink, Outlet } from "react-router";
import type { Route } from "./+types/layout";
import { getContacts } from "~/contacts.server";
export async function loader() {
return { contacts: await getContacts() };
}
export default function ContactsLayout({ loaderData }: Route.ComponentProps) {
return (
<div style={{ display: "flex", gap: "1rem" }}>
<nav>
<ul>
{loaderData.contacts.map((contact) => (
<li key={contact.id}>
<NavLink to={`/contacts/${contact.id}`}>{contact.name}</NavLink>
</li>
))}
</ul>
</nav>
<section>
<Outlet />
</section>
</div>
);
}The sidebar and its loader belong to the layout. When the user moves from /contacts/1 to /contacts/2, only the child route changes. The layout stays mounted, its state is preserved, and its loader is not rerun by the navigation alone (it reruns after actions or when its dependencies change). This is the main practical benefit of nesting.
The root route works the same way. Its component renders <Outlet /> inside the document, and every other route appears there.
Passing data to children
A parent can hand values to its children through the context prop of Outlet. Children read them with useOutletContext.
import { Outlet, useOutletContext } from "react-router";
type ContactsContext = { canEdit: boolean };
export function ParentExample() {
return <Outlet context={{ canEdit: true } satisfies ContactsContext} />;
}
export function ChildExample() {
const { canEdit } = useOutletContext<ContactsContext>();
return <p>{canEdit ? "You can edit." : "Read only."}</p>;
}useOutletContext is not type-checked against what the parent passed, so the generic is a promise you make. For data fetched on the server, prefer a loader in the route that needs it. Use outlet context only for small values that the parent already has.
Common errors
- Missing
Outlet. If a parent has children but never renders<Outlet />, the child matches but nothing appears. This is the most frequent cause of "my route is blank". - Missing index route.
/contactsrenders the layout with an empty content area. Add anindexchild, or use the area for a placeholder. - Path collisions. Two routes that resolve to the same full path make the match ambiguous. Give each URL one route module.
- Wrong file path. Paths in
routes.tsare relative toapp/. A typo causes a build error naming the missing file. - Expecting parent loaders to rerun on every navigation. They do not. A loader reruns when its route's params change, after an action, or when you revalidate.
Exercise
You configure route("contacts", "routes/contacts/parent.tsx", [index("routes/contacts/list.tsx")]). Visiting /contacts shows the parent's heading but no list. What is the most likely cause, and how do you fix it?
Show answer
The parent component does not render <Outlet />, so the matched index child has no place to appear. Add <Outlet /> where the list should go in parent.tsx.
Exercise
You want /login and /signup to share a centered card wrapper, without a /auth prefix in the URL. Which routes.ts helper do you use?
Show answer
Use layout("routes/auth-layout.tsx", [route("login", "routes/login.tsx"), route("signup", "routes/signup.tsx")]). The layout adds no URL segment, and its component renders <Outlet /> inside the card.