Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 50 ยท Projects

Project: A Multi-Page Site with Nested Layouts

Build a small multi-page site step by step: a route table, active navigation links, nested layouts with Outlet, URL params, breadcrumbs, page titles and a 404 inside the layout.

Most websites are many pages that share the same frame: a header, a navigation bar, a footer and, in some sections, a sidebar. Routing is how you describe that structure once instead of repeating it on every page. In this project you will build a small company site with a home page, an about page, a documentation section with a sidebar, a contact page and a friendly 404, and you will see how a route tree produces all of it.

The project uses everything from the first chapters: route matching, NavLink, 404 routes, nested routes with Outlet, layout routes, URL params and a taste of route handles for breadcrumbs. React Router itself cannot be installed in the live editor, so the runnable code uses the small hand-written router from those lessons, built on React and location.hash. Its names (NavLink, Outlet, useParams, route objects with children) match the real library, and each step shows the real React Router equivalent as read-only code. The real version of this site would run as is by swapping the stand-in for createBrowserRouter and RouterProvider.

What we are building

flowchart TD
    A["SiteLayout<br/>header, nav, breadcrumbs, footer"] --> B["index: Home"]
    A --> C["about: About"]
    A --> D["docs: DocsLayout<br/>sidebar of guides"]
    A --> E["contact: Contact"]
    A --> F["path *: NotFound"]
    D --> G["index: pick a guide"]
    D --> H["path :slug: one guide"]

Every page renders inside SiteLayout; guides additionally render inside DocsLayout. The 404 keeps the site frame.

Planning the tree first is the best habit in routing work. Ask three questions of every page: what URL does it have, which frame does it live in, and which parts of the URL are data (params)? The answers give you the tree above. The same tree in real React Router is:

JSX
// routes.jsx - React Router (read-only)
import { createBrowserRouter } from "react-router";

export const router = createBrowserRouter([
  {
    path: "/",
    Component: SiteLayout,
    handle: { crumb: () => "Home", title: () => "Acme" },
    children: [
      { index: true, Component: Home },
      { path: "about", Component: About, handle: { crumb: () => "About" } },
      {
        path: "docs",
        Component: DocsLayout,
        handle: { crumb: () => "Docs" },
        children: [
          { index: true, Component: DocsIndex },
          { path: ":slug", Component: DocPage },
        ],
      },
      { path: "contact", Component: Contact, handle: { crumb: () => "Contact" } },
      { path: "*", Component: NotFound },
    ],
  },
]);

Start with the smallest useful piece: a navigation bar that knows which page you are on. The stand-in NavLink reads the current path from the hash and marks its own link with aria-current="page", which is also what the real NavLink does. Screen readers announce it, and CSS can style it with [aria-current="page"]. The end prop matters for the Home link: without it, / would count as a prefix of every URL and Home would always look active.

Step 1: a flat site with active links

Click through the pages: exactly one link is highlighted. Remove the word end from the Home link and watch Home stay highlighted on every page. This flat table cannot express a sidebar that only exists in one section, though, which is why the next step introduces nesting.

Step 2: nested layouts, params and the 404

Now the whole site. Read the code in four parts.

The matcher. resolve walks the route tree and returns the chain of matched routes, from the outermost layout to the page. Each match carries its params and the part of the URL it consumed (pathname), which the breadcrumbs need later.

Layouts and Outlet. A layout is a component with an <Outlet /> marker where its child route renders. SiteLayout renders the header, the breadcrumbs and the footer around the outlet. DocsLayout renders the sidebar and puts the selected guide in its own outlet. Because docs is a child of /, a guide page is wrapped by both layouts automatically.

Params. The :slug route reads useParams() and looks the guide up in a list. If nothing matches, the page prints a short message inside the docs layout, so the sidebar stays and the visitor can pick another guide. This is the pattern for "valid pattern, missing record". It is a not-found, but a local one.

The handle. Each route may carry a handle object with a crumb and a title function. The breadcrumb trail is built from the chain of matches that have a crumb, and the document title comes from the deepest match that defines one. Neither component needs to know about the other pages. Adding a page means adding a route with a handle.

Step 2: the full site

Walk through the checks that prove the structure works:

  • Open Docs and then Installation. The breadcrumb becomes Home / Docs / Installation, the sidebar link is highlighted and the footer shows the tab title Installation - Acme Docs. The sidebar did not remount between guides: only the outlet content changed.
  • Click bad guide (/docs/missing). The sidebar and header stay, the content area says there is no such guide, and the breadcrumb ends in "Not found". This is a nested, local "not found".
  • Click bad page (/careers). The catch-all route renders the 404 inside SiteLayout, so the navigation is still there.
  • Open About. The Docs link is not highlighted, because /about does not start with /docs/.

What to notice about the design

  • Structure lives in one data structure. Adding a "Pricing" page is one object in the children array and one link. No other file changes.
  • Layouts have no knowledge of their pages. SiteLayout never lists routes. It only renders Outlet. The nav is separate because navigation links are content, not routing.
  • Where an error belongs depends on the nesting. The missing guide is handled by the page that owns the param, and the unknown URL by the catch-all. Neither handler can see the other's cases.
  • The handle is plain data. Breadcrumbs and titles are computed from matches, so they cannot go out of sync with the URL.
  • end matters for index links. Any link whose target is the parent of other links needs it.

One thing the stand-in does not model is the real router's ranking: the library scores patterns by specificity, so route order does not matter there, while the stand-in uses the first match. If you port this site to React Router, you can drop any ordering workarounds, but nothing else about the tree changes.

Extend the project

Try these extensions on your own. Each one uses an idea from the track:

  1. Add a pricing page and a Pricing NavLink. How many places did you edit?
  2. Make the guides data-driven with a loader-like function and show a loading state (Loaders).
  3. Add a redirect from /documentation to /docs (Redirects).
  4. Set the focus to the page heading after each navigation for keyboard users (Scroll, Focus and Page Titles).
  5. Add a second layout (for example a bare layout without header for /contact) using a pathless route (Layout Routes).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the Home NavLink need the end prop?

    NavLink treats a link as active when the current URL equals or starts with its target. Every path starts with /, so end limits the match for the root link to the exact URL.
  2. 2. A guide URL such as /docs/install renders inside two layouts. Why?

    The matched chain is SiteLayout, DocsLayout, DocPage. Each layout renders an Outlet where the next route in the chain appears.
  3. 3. Where should the 'no such guide' message be rendered so the sidebar stays visible?

    The pattern /docs/:slug matched, so the page itself decides what to show when the record is missing. It renders inside DocsLayout, which keeps the sidebar.
  4. 4. How are the breadcrumbs in this project built?

    The router already knows the chain of matches for the URL. Reading handle.crumb from each match keeps the trail in sync with the route tree.

Summary

  • Plan the route tree first: URL, frame (layout) and params for every page.
  • NavLink marks the current page with aria-current="page"; use end for links that are prefixes of others.
  • Layout routes render an Outlet, so nested routes get their frames for free, and a catch-all inside a layout keeps the frame on a 404.
  • A missing record behind a valid pattern is handled by the page that reads the param, so the surrounding layout survives.
  • Route handle data gives you breadcrumbs and page titles derived from the matched chain.

Next: Project: A Blog with Loaders and Search Params