React / 37
Layouts with nested routes & Outlet
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:
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: truemarks the default child, shown at the parent's own URL (/). An index route has nopathand can't have children.- One
errorElementon the parent now covers every child, plus unknown URLs. menu/:idis a dynamic segment: it matches/menu/7,/menu/42and so on. Next topic.
The layout and its Outlet
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 itWelcome 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 />
| URL | What renders |
|---|---|
/ | AppLayout with Home in the Outlet |
/menu | AppLayout with Menu in the Outlet |
/menu/7 | AppLayout with Dish in the Outlet (id is "7") |
/oops | ErrorPage 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: show where you are
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:
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.
