Skip to content
ClapelloNotes by Barath
ReactCh 07 · 4/6
Core4 min readUpdated

React / 37

Layouts with nested routes & Outlet

TL;DR

Put shared UI like the header in a parent route, render child routes into its <Outlet />, and highlight the current page with NavLink.

The route tree

Make the layout the root route and put the pages in its children:

main.jsx
const router = createBrowserRouter([
  {
    path: "/",
    element: <AppLayout />,
    errorElement: <ErrorPage />,
    children: [
      { index: true, element: <Home /> },
      { path: "menu", element: <Menu /> },
      { path: "menu/:id", element: <Dish /> },
      { path: "about", element: <About /> },
    ],
  },
]);
  • Child paths are relative: "menu", not "/menu". The parent's path is added for you, so it matches /menu.
  • index: true marks the default child, shown at the parent's own URL (/). An index route has no path and can't have children.
  • One errorElement on the parent now covers every child, plus unknown URLs.
  • menu/:id is a dynamic segment: it matches /menu/7, /menu/42 and so on. Next topic.

The layout and its Outlet

AppLayout.jsx
import { NavLink, Outlet } from "react-router";
 
export default function AppLayout() {
  return (
    <>
      <header>
        <nav>
          <NavLink to="/">Home</NavLink>
          <NavLink to="/menu">Menu</NavLink>
          <NavLink to="/about">About</NavLink>
        </nav>
      </header>
 
      <main>
        <Outlet />
      </main>
 
      <footer>© Tasty Bites</footer>
    </>
  );
}

<Outlet /> is where the matching child renders: <Home /> at /, <Menu /> at /menu, and so on. Try it:

URL → matched routes → Outlet

Try it
localhost:5173/
AppLayout
<Outlet /> = <Home />

Welcome to Tasty Bites. Pick something from the menu.

or type a URL

createBrowserRouter([ … ])

{ path: "/", element: <AppLayout />, errorElement: <ErrorPage />, children: [ { index: true, element: <Home /> }, { path: "menu", element: <Menu /> }, { path: "menu/:id", element: <Dish /> }, { path: "about", element: <About /> },] }

Matched: AppLayout → <Home />

Click around, or jump straight to a URL. The header (AppLayout) stays put and only the Outlet's content swaps. /oops matches nothing, so the root's errorElement replaces the whole layout.
URLWhat renders
/AppLayout with Home in the Outlet
/menuAppLayout with Menu in the Outlet
/menu/7AppLayout with Dish in the Outlet (id is "7")
/oopsErrorPage only (no match, so the layout is replaced)

Children can have their own children, and their own <Outlet />, as deep as your UI needs: an account page with tabs is a layout inside a layout.

NavLink is a Link that knows whether it matches the current URL. When it does, it automatically gets the class active and aria-current="page", so styling the current page is plain CSS:

AppLayout.css
nav a.active {
  font-weight: 600;
  text-decoration: underline;
}

Or decide the class yourself. className can be a function that receives isActive:

<NavLink to="/menu" className={({ isActive }) => (isActive ? "tab tab-on" : "tab")}>
  Menu
</NavLink>

By default /menu stays active on /menu/7 too, because it matches the start of the URL, which is usually what you want for a section. Add the end prop to make it active only on exactly /menu. <NavLink to="/"> is special: it's only active on / itself, otherwise it would match every page.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.