Sub topic 5
Nested routes and shared layouts with Outlet
This chapter assumes you know React function components, JSX, and how to define a router with createBrowserRouter and RouterProvider. It also assumes you can write a basic route object with a path and an element. Here you will learn how nested routes share a layout, and how Outlet decides where a child route renders.
Why nest routes
Most apps repeat the same page frame on many URLs, such as a header, a navigation bar, and a footer. Copying that frame into every page component is error-prone. Nested routes solve this. A parent route renders the shared frame, and its child routes render inside it.
Nesting also mirrors the URL. A child's path is appended to its parent's path, so a parent at / with a child about matches /about.
The parent, the children, and Outlet
A route object takes a children array. When a URL matches a child, the router renders the parent's element. It then renders the child's element at the place where the parent calls <Outlet />.
import { createBrowserRouter, RouterProvider, Link, Outlet } from "react-router-dom";
function Layout() {
return (
<div>
<nav>
<Link to="/">Home</Link> <Link to="/about">About</Link>
</nav>
<Outlet />
</div>
);
}
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{ index: true, element: <h1>Home</h1> },
{ path: "about", element: <h1>About</h1> },);
export default function App() {
return <RouterProvider router={router} />;
}Layout stays mounted while you move. Only the content at <Outlet /> changes. The nav` is not re-created, so any state inside it is kept.
The route { index: true } is an index route. It renders at the parent's own URL, here /, when no other child matches. The next chapter covers index routes in more detail.
How a match becomes a tree
The router matches the URL against the whole route tree and builds a list of matching routes from the outermost to the innermost. Each Outlet renders the next route in that list.
Layouts without a path
A parent does not need a path. A route with only element and children is called a pathless layout route. It adds a shared frame without adding a URL segment.
const router = createBrowserRouter([
{ path: "/login", element: <h1>Login</h1> },
{
element: <Layout />,
children: [
{ path: "/", element: <h1>Home</h1> },
{ path: "/about", element: <h1>About</h1> },
],
},
]);Here /login has no layout, while / and /about share Layout. Child paths that start with / are absolute, so they are written in full.
Common errors
- Missing
Outlet. If the parent never renders<Outlet />, the child matches but nothing appears. The page shows only the parent. - Leading slash on child paths. A child path of
/aboutis absolute and ignores the parent's path. Useaboutwhen you want it relative to the parent. - No index route. Visiting the parent's exact URL renders the layout with an empty
Outlet. Add an index child to fill it.
Exercise
A parent route at path "/dashboard" has the element <DashboardLayout /> and one child with path "settings". Which URL shows the settings page, and where does its content appear inside DashboardLayout?
Show answer
The URL is /dashboard/settings. The settings element appears wherever DashboardLayout renders <Outlet />. If DashboardLayout does not render an Outlet, the settings content does not appear.