Sub topic 3
Defining routes in app/routes.ts
This chapter covers how React Router framework mode maps URLs to route module files. It assumes you can read TypeScript and know what a React component is. You should also know how a URL path is built from segments and that a project from the previous chapter exists, with an app/ directory and a routes.ts file inside it.
What routes.ts does
In framework mode, URL matching is not inferred from component code. You describe it in one file, app/routes.ts. That file default-exports an array of route entries. Each entry pairs a URL pattern with the path of a route module, which is a file that exports a component and optional functions such as loader and action.
The compiler reads this file at build time and in the dev server. It uses the array to decide which modules to bundle for a given URL and which types to generate. Paths to module files are relative to the app/ directory.
The helper functions
The file imports helpers from @react-router/dev/routes:
route(path, file, children?)maps a URL pattern to a module.index(file)is the default child of its parent and renders at the parent's exact URL.layout(file, children)wraps its children in a shared parent without adding a URL segment.prefix(path, routes)adds a path prefix to a group of routes without creating a parent module.
Here is a complete app/routes.ts:
import {
type RouteConfig,
index,
route,
layout,
} from "@react-router/dev/routes";
export default [
index("routes/home.tsx"),
layout("routes/contacts/layout.tsx", [
route("contacts", "routes/contacts/list.tsx"),
route("contacts/new", "routes/contacts/new.tsx"),
route("contacts/:contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;satisfies RouteConfig checks the array's shape at compile time and keeps the literal types. Using : RouteConfig also works, but satisfies is the convention in the generated templates.
Reading the example
The first entry, index("routes/home.tsx"), renders at /. It has no path because an index route inherits its parent's path. Here the parent is the root route, so the index matches /.
The layout entry has no path. Its module, routes/contacts/layout.tsx, renders around whichever child matches. The three route calls inside it each have a full path. Because the layout adds no URL segment, the paths still begin with contacts.
| URL | Matched module | Wrapped by |
|---|---|---|
| `/` | `routes/home.tsx` | root only |
| `/contacts` | `routes/contacts/list.tsx` | contacts layout |
| `/contacts/new` | `routes/contacts/new.tsx` | contacts layout |
| `/contacts/42` | `routes/contacts/detail.tsx` | contacts layout |
Every route is also wrapped by the root route in app/root.tsx, which the next chapter covers.
Dynamic segments
A segment that starts with a colon, such as :contactId, is a dynamic segment. It matches any single non-empty segment and exposes the matched text as a string in params. For /contacts/42, params.contactId is "42". The value is always a string, so convert it yourself if you need a number.
Both /contacts/new and /contacts/:contactId could match the URL /contacts/new. React Router ranks static segments above dynamic ones, so new wins regardless of the order in the array. You do not need to list the specific route first.
Other pattern features:
- An optional segment ends with
?, as inroute(":lang?/about", ...). - A splat, written
*, matches the rest of the URL. Its value is inparams["*"].
Nesting with children
The third argument to route is an array of children. The parent module renders its children through <Outlet />, covered in a later chapter. Here is a nested version:
import { type RouteConfig, route, index } from "@react-router/dev/routes";
export default [
route("dashboard", "routes/dashboard/layout.tsx", [
index("routes/dashboard/overview.tsx"),
route("settings", "routes/dashboard/settings.tsx"),
]),
] satisfies RouteConfig;Child paths are relative to the parent. This config yields /dashboard for the overview and /dashboard/settings for settings. The difference from layout is that route contributes a URL segment and layout does not.
Grouping with prefix
prefix is useful when several routes share a path but you do not want a parent component:
import {
type RouteConfig,
route,
prefix,
} from "@react-router/dev/routes";
export default [
...prefix("admin", [
route("users", "routes/admin/users.tsx"),
route("audit", "routes/admin/audit.tsx"),
]),
] satisfies RouteConfig;prefix returns an array, so spread it into the config. The URLs are /admin/users and /admin/audit. There is no /admin route and no extra layer in the tree.
Common errors
- Wrong file path. Paths are relative to
app/, not toroutes.ts. Writing"./routes/home.tsx"or"app/routes/home.tsx"fails to resolve the module. - Forgetting the spread on
prefix. Without..., you nest an array inside the config and the build rejects it. - Expecting
layoutto add a URL. A layout has no path. If you want/contactsto exist, add aroute("contacts", ...)inside it. - Duplicate index routes. Two
indexentries under the same parent conflict, because both claim the same URL. - Assuming files in
routes/register themselves. Putting a file in the folder does nothing. Only entries inroutes.tsbecome routes.
Generating the config from files
Because routes.ts is ordinary TypeScript, you can compute the array. The package @react-router/fs-routes provides flatRoutes(), which builds the config from file names in app/routes/. You can also mix it with manual entries:
import { type RouteConfig, route } from "@react-router/dev/routes";
import { flatRoutes } from "@react-router/fs-routes";
export default [
route("health", "health.tsx"),
...(await flatRoutes()),
] satisfies RouteConfig;The explicit config is easier to search and review, and the file-based approach saves typing in large apps. Choose one style per app unless you have a reason to mix them.
Effect on generated types
Each entry in routes.ts gets a generated types file under .react-router/types/, imported as ./+types/<module name>. The types know the route's params. For contacts/:contactId, params.contactId is typed as string. If you change a path in routes.ts, run react-router typegen, or restart the dev server, so the types update.
Exercise
You want /admin/users and /admin/audit to share a layout component at routes/admin/layout.tsx, but no page should exist at /admin itself. Write the routes.ts entry. Which helper adds the URL segment, and why do you not need an index route?
Show answer
Use route("admin", "routes/admin/layout.tsx", [route("users", "routes/admin/users.tsx"), route("audit", "routes/admin/audit.tsx")]). The route helper adds the "admin" segment and the layout module renders the children through Outlet. An index route is optional. Without one, the URL /admin matches the layout with no child, so the Outlet renders nothing there. Add an index only if /admin should show content.
Exercise
Given route("contacts/:contactId", "routes/contacts/detail.tsx") and route("contacts/new", "routes/contacts/new.tsx") in that order, which module handles /contacts/new, and what type is params.contactId for /contacts/7?
Show answer
routes/contacts/new.tsx handles it, because static segments rank above dynamic ones regardless of array order. params.contactId is the string "7".