Route Configuration
React Router v7 uses a configuration-based approach to defining routes through theroutes.ts file located in your app directory. This provides type safety, flexibility, and better maintainability compared to convention-based routing.
Basic Configuration
Every React Router application requires aroutes.ts file that exports an array of route configuration objects:
Route Config Helpers
React Router provides helper functions to define routes in a type-safe, declarative way:route()
Defines a standard route with a path and component file:
index()
Defines an index route that renders at the parent’s path:
layout()
Defines a layout route (no path segment) that wraps child routes:
prefix()
Adds a path prefix to a group of routes without requiring a parent component:
Route Configuration Options
Each route config entry supports the following options:| Option | Type | Description |
|---|---|---|
file | string | Path to the route module (relative to app directory or absolute) |
path | string | URL path pattern to match |
id | string | Unique identifier for the route (auto-generated from file path if not provided) |
index | boolean | Whether this is an index route |
caseSensitive | boolean | Whether path matching is case-sensitive (default: false) |
children | RouteConfigEntry[] | Nested child routes |
File-Based Routing
While manual configuration is recommended, you can use file-based routing conventions via the@react-router/fs-routes package:
app/routes/ directory. See File Conventions for details.
Absolute File Paths
You can use absolute paths for route modules:Relative Scoping
For splitting route configuration across multiple files, use therelative() helper:
Route Modules
Each route file exports React components and data functions:Best Practices
- Use helpers: Always use
route,index, andlayouthelpers instead of raw objects - Type safety: Use
satisfies RouteConfigto ensure type checking - Relative paths: Prefer relative file paths (
"routes/about.tsx") over absolute paths - Logical grouping: Organize routes by feature or domain, not just URL structure
- Avoid deep nesting: Keep route hierarchies shallow when possible
Migration from Remix
If migrating from Remix with theroutes option, use the adapter: