Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 50 ยท Nested Routes and Layouts

Layout Routes and Pathless Routes

Share headers, sidebars and guards between pages with layout routes that add no URL segment: pathless routes, several layouts in one app and route groups.

In the previous lesson every parent route had a path: users was the parent of :userId, and the parent's path became part of the URL. But layouts and URLs do not always line up. Your marketing pages (/, /about, /pricing) want a big header and a footer. Your app pages (/dashboard, /settings) want a sidebar. Your login and sign-up pages want a small centered card. None of these groups has a common URL prefix, and you certainly do not want to invent ugly addresses such as /marketing/about just to share a layout.

The answer is a layout route: a route that renders a component with an <Outlet /> but has no path. It is also called a pathless route. It takes part in the match chain and wraps its children, yet it adds nothing to the URL. In this lesson you will build three layouts side by side, stack layouts inside each other, and use a pathless route to hold shared logic. If you have not read Nested Routes and Outlet, start there.

A route without a path

This is real React Router code (read-only, because the live editor cannot install packages):

JSX
// App.jsx - React Router (read-only)
import { Routes, Route } from 'react-router';

export default function App() {
  return (
    <Routes>
      <Route element={<MarketingLayout />}>
        <Route index element={<Home />} />
        <Route path="about" element={<About />} />
        <Route path="pricing" element={<Pricing />} />
      </Route>

      <Route element={<AppLayout />}>
        <Route path="dashboard" element={<Dashboard />} />
        <Route path="settings" element={<Settings />} />
      </Route>

      <Route element={<AuthLayout />}>
        <Route path="login" element={<Login />} />
        <Route path="signup" element={<Signup />} />
      </Route>
    </Routes>
  );
}

The three outer <Route> elements have an element but no path. Their children keep their own paths, so the URLs are simply /, /about, /pricing, /dashboard, /settings, /login and /signup. Each layout component renders its own chrome and an <Outlet />, exactly as in any nested route.

The same tree as plain route objects (see Routes as Objects):

JSX
// routes.jsx - React Router (read-only)
export const routes = [
  {
    element: <MarketingLayout />,
    children: [
      { index: true, element: <Home /> },
      { path: 'about', element: <About /> },
      { path: 'pricing', element: <Pricing /> },
    ],
  },
  {
    element: <AppLayout />,
    children: [
      { path: 'dashboard', element: <Dashboard /> },
      { path: 'settings', element: <Settings /> },
    ],
  },
];
flowchart TD
    R["Router"] --> M["MarketingLayout<br/>(no path)"]
    R --> A["AppLayout<br/>(no path)"]
    R --> U["AuthLayout<br/>(no path)"]
    M --> M1["/ (index)"]
    M --> M2["/about"]
    A --> A1["/dashboard"]
    A --> A2["/settings"]
    U --> U1["/login"]
    U --> U2["/signup"]

Layouts group routes without touching URLs. A page picks its layout by where it sits in the tree.

Real library or stand-in? The code above is the real API. The live examples use a small hand-written router (route objects, a matcher that returns the whole chain, and an Outlet component) built on React and location.hash, since the sandboxed editor has no packages and no History API. The nesting and pathless behavior shown is the same as in React Router; ranking, relative links and loaders are not part of the stand-in.

Three layouts, no URL prefixes

Move between "Home", "Open app" and "Log in". The look changes completely (a blue header and footer, a green sidebar, a purple centered card), yet the URL line only ever shows the page's own path. Then click "Broken link": the unknown address is caught by the * route inside the marketing layout, so the 404 still gets a header and footer. The pages in the other two layouts have no catch-all of their own, which is a choice you make per group.

Stacking layouts

Layouts can be nested to any depth, and you can freely mix layouts that have paths with layouts that do not. A typical documentation site might have a site-wide header (pathless), a docs section with a sidebar (path docs), and inside that a group of generated reference pages that share a banner (pathless again). The pathless routes contribute components but not URL segments.

Layouts that add no URL segment

Open "fetch()". The URL is /docs/api/fetch, but the chain has four entries: site, docs, reference-group and fetch. The first and third have no path, so they do not appear in the URL, yet both draw their component. Compare "Intro": it skips the dashed banner, because intro is a sibling of the group, not a child of it. That is a route group: the same idea as folder groups in some file-based frameworks, expressed as a pathless route.

Pathless routes for shared logic

A layout route does not have to draw anything. Its component can simply decide whether to render its children, which makes it a convenient place for logic that must apply to a whole group of URLs: a login check, a feature flag, a provider that only some pages need, or an error boundary. The next stand-in uses a toggle for "signed in" and a pathless route that either renders the Outlet or explains why it will not.

A pathless route that guards a group

Open "Inbox" while signed out and you get the message. Click "Sign in" and the same URL now shows the inbox, because the guard's component re-rendered and chose to render its Outlet. The URLs /inbox and /profile contain no trace of the guard. Adding a third protected page means adding one more child. This is the shape of the real protected-route pattern, built out properly (with redirects to a login page and a "return to where you were" state) in Protected Routes and Auth Redirects.

A guard in the browser only controls what the interface shows. Everything shipped to the browser can be read or changed by the user, so the server must still check permissions on every request for protected data.

Layout routes in framework mode

In React Router's framework mode, routes are declared in a routes.ts file, and the same idea has a helper called layout(). It takes the layout's module file and a list of children, with no URL path:

TypeScript
// app/routes.ts - React Router framework mode (read-only)
import { type RouteConfig, index, layout, route } from '@react-router/dev/routes';

export default [
  layout('./layouts/marketing.tsx', [
    index('./routes/home.tsx'),
    route('about', './routes/about.tsx'),
  ]),
  layout('./layouts/app.tsx', [
    route('dashboard', './routes/dashboard.tsx'),
  ]),
] satisfies RouteConfig;

The ideas carry over unchanged. Route Config and File Routes covers it in detail.

Choosing the right kind of parent

You want Use
A shared layout and a common URL prefix (/account/...) A route with a path and an element that renders <Outlet />
A shared layout and no URL prefix A route with an element and no path
A common URL prefix and no layout A route with a path and no element
Shared logic (guard, provider, boundary) for a group A pathless route whose component renders <Outlet /> conditionally

Keep one rule in mind: every layout, pathless or not, must render <Outlet /> where its children go. For passing values from a layout down to its children, continue with Passing Data Down with Outlet Context.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a route a 'layout route' (pathless route)?

    A pathless route has an element (usually containing <Outlet />) and no path. It wraps its children without adding a URL segment.
  2. 2. A pathless layout route wraps a child with path='about'. What is the URL of that page?

    Pathless routes contribute no segment, so the child's URL is just /about.
  3. 3. You want /login and /signup to share a small centered card, but /dashboard and /settings to share a sidebar, with no extra URL prefixes. What do you do?

    One pathless layout route per group keeps the URLs clean and the layout markup in one place.
  4. 4. Why is a pathless guard route (such as RequireSignIn) not enough to protect private data?

    Client code is visible and modifiable. A guard improves the user experience, but real protection requires the server to check permissions on each request.

Summary

  • A layout route has an element (which renders <Outlet />) and no path; it wraps children without changing the URL.
  • Use one pathless layout per group of pages (marketing, app, auth) to give each group its own header, sidebar or card.
  • Layouts stack: pathless and path-based parents can be mixed to any depth, and pathless routes still appear in the match chain.
  • A pathless route can hold shared logic, such as a sign-in guard, which is only a user-interface convenience, not security.
  • Put a * route inside a layout to give 404 pages that layout; framework mode offers the same idea through layout() in routes.ts.

Next: Passing Data Down with Outlet Context