Chuyển đến nội dung

Bài 10 · Routes and Nested Layouts

Index, Layout and Pathless Routes

Understand index routes, layout routes without a path and optional segments in React Router v7 framework mode, and how to share UI without changing the URL.

Nested routes raise a few practical questions. What shows in the <Outlet /> at /dashboard itself, where no child matches? How do you share a sidebar between pages without adding a URL segment? How do you make a segment optional? Index routes, layout routes and optional segments answer these.

The code below is read-only React Router config. The stand-ins are plain JavaScript that model how a route tree turns into a list of matches.

Index routes

An index route is the default child of its parent: it renders in the parent's outlet when the URL matches the parent exactly.

TypeScript
// app/routes.ts
import { type RouteConfig, route, index } from "@react-router/dev/routes";

export default [
  route("dashboard", "routes/dashboard.tsx", [
    index("routes/dashboard-home.tsx"),
    route("settings", "routes/dashboard-settings.tsx"),
  ]),
] satisfies RouteConfig;

At /dashboard the matches are dashboard then dashboard-home. At /dashboard/settings they are dashboard then dashboard-settings. Without the index route, /dashboard would render the layout with an empty outlet.

An index route has no path and no children. It also has a special role in forms: a <Form> rendered inside an index route posts to the index (?index), so its action runs there and not in the parent.

Layout routes

A layout route has no path. It adds UI, loader data and error boundaries around its children without adding a URL segment.

TypeScript
import { type RouteConfig, route, index, layout, prefix } from "@react-router/dev/routes";

export default [
  layout("routes/marketing-layout.tsx", [
    index("routes/home.tsx"),
    route("about", "routes/about.tsx"),
    route("pricing", "routes/pricing.tsx"),
  ]),
  layout("routes/app-layout.tsx", [
    ...prefix("app", [
      index("routes/app-home.tsx"),
      route("projects", "routes/projects.tsx"),
    ]),
  ]),
] satisfies RouteConfig;

/, /about and /pricing share the marketing layout; /app and /app/projects share the app layout. The URLs show no marketing or layout segment. A layout route is also the natural place for an auth check, because its loader runs for every child (see Protecting Routes).

JS Stand-in: flatten a route tree into URL patterns and match chains

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Optional segments

A ? after a segment makes it optional, so one route module serves several URLs:

TypeScript
route(":lang?/categories", "routes/categories.tsx"),
route("users/:userId/edit?", "routes/user.tsx"),

The first matches /categories and /en/categories; params.lang is undefined in the first case. Always handle the missing value in code, for example params.lang ?? "en".

JS Stand-in: optional segments expand to several patterns

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Which one do I need?

flowchart TD
  A["Need shared UI or data?"] -->|"yes, and the URL has a shared prefix"| B["route(path, file, children)"]
  A -->|"yes, but no URL prefix"| C["layout(file, children)"]
  A -->|"default content at the parent URL"| D["index(file)"]
  A -->|"segment may be missing"| E["optional segment with ?"]

Choose the helper that matches whether the URL changes.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What renders in the parent's <Outlet /> at the parent's exact URL if there is no index route?

    Without an index route no child matches, so the outlet is empty.
  2. 2. What is special about a layout route in routes.ts?

    layout() takes a file and children but no path.
  3. 3. Which pattern matches both /blog and /en/blog?

    The question mark makes the :lang segment optional.
  4. 4. Can an index route have child routes?

    An index route renders at the parent's URL and cannot nest further routes.

Summary

  • index() is the default child at the parent's URL.
  • layout() shares UI and data without adding a path segment.
  • ? makes a segment optional; handle undefined params.
  • Pick the helper by asking whether the URL should change.

Next: Loader Basics

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.