Skip to main content

Route Configuration

React Router v7 uses a configuration-based approach to defining routes through the routes.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 a routes.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:

File-Based Routing

While manual configuration is recommended, you can use file-based routing conventions via the @react-router/fs-routes package:
This automatically discovers routes based on files in the 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 the relative() helper:

Route Modules

Each route file exports React components and data functions:

Best Practices

  1. Use helpers: Always use route, index, and layout helpers instead of raw objects
  2. Type safety: Use satisfies RouteConfig to ensure type checking
  3. Relative paths: Prefer relative file paths ("routes/about.tsx") over absolute paths
  4. Logical grouping: Organize routes by feature or domain, not just URL structure
  5. Avoid deep nesting: Keep route hierarchies shallow when possible

Migration from Remix

If migrating from Remix with the routes option, use the adapter: