Sub topic 3
Defining routes in app/routes.ts
This chapter assumes you can read TypeScript and React components, and that you have created a framework-mode project with app/root.tsx and app/routes.ts. In framework mode, app/routes.ts is the single place where URL paths are tied to route module files.
What the file exports
app/routes.ts has a default export: an array of route entries. React Router reads it at build time and when the dev server starts. You build entries with helper functions from @react-router/dev/routes.
| Helper | Purpose |
|---|---|
| `index(file)` | Renders at the parent's exact URL, such as `/` at the top level. |
| `route(path, file, children?)` | Matches a path segment and renders the module at `file`. |
| `layout(file, children)` | Wraps child routes in a shared parent. It adds no URL segment. |
File paths are relative to the app directory.
A working example
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;Reading it top to bottom:
index("routes/home.tsx")serves/.layout(...)wraps three routes inroutes/contacts/layout.tsx. The URL is not affected, so the list is still at/contacts.:contactIdis a dynamic segment. Its value reaches the route module asparams.contactId.satisfies RouteConfigchecks the array's type without widening it.
Nesting with a path prefix
route accepts a third argument, an array of children. Child paths are appended to the parent's path, and the child renders inside the parent's Outlet. This is useful when routes share a prefix and a UI shell:
import { type RouteConfig, route, index } from "@react-router/dev/routes";
export default [
route("contacts", "routes/contacts/layout.tsx", [
index("routes/contacts/list.tsx"),
route("new", "routes/contacts/new.tsx"),
route(":contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;This produces the same three URLs as the first example. The difference is that the parent now owns the /contacts segment, and the list becomes an index route.
Common errors
- Missing file. A path that does not exist under
app/fails the build or dev server. Check the spelling and the extension. - Forgetting
index. A parent with children but no index route renders only its layout at the parent URL. - Expecting
layoutto change the URL. It never adds a segment. Useroutewith a path if you want one. - Duplicate paths. Two routes with the same full path are ambiguous. Keep each URL defined once.
Exercise
You want /dashboard/settings and /dashboard/profile to share a sidebar component, but the sidebar route itself should not add a URL segment. Which helper wraps the two routes, and what are the two route paths?
Show answer
Use layout("routes/dashboard/sidebar.tsx", [...]) around route("dashboard/settings", "routes/dashboard/settings.tsx") and route("dashboard/profile", "routes/dashboard/profile.tsx"). The layout adds no segment, so the full paths come from the route calls.