Core Routing Concepts
React Router’s routing system is built on a foundation of nested route matching, enabling you to build complex application layouts with intuitive URL structures.Route Matching
At its core, React Router matches URL paths to route definitions and renders the appropriate components. The router uses a nested matching algorithm that allows parent routes to render layouts while child routes render specific content.Basic Route Configuration
Routes can be defined declaratively using JSX or programmatically using route objects:Nested Routes
Nested routes allow you to compose UI hierarchies that mirror your URL structure. Parent routes render an<Outlet /> where child routes appear.
Dynamic Segments
Dynamic segments capture values from the URL and make them available as params:Multiple Dynamic Segments
Splat Routes (Catch-all)
Splat routes capture the rest of the URL:Index Routes
Index routes render when a parent route matches exactly:Route Matching Algorithm
The router matches routes using thematchRoutes function from lib/router/utils.ts. The algorithm:
- Normalizes the pathname - Removes basename and decodes URI components
- Ranks routes - Routes are ranked by specificity (static > dynamic > splat)
- Matches segments - Each URL segment is matched against route patterns
- Returns matches - Returns an array of matching routes from parent to child
Path Matching Patterns
Static Paths
Dynamic Segments
Optional Segments
Splat Routes
Case Sensitivity
By default, routes are case-insensitive. You can make them case-sensitive:Route Resolution
The router resolves relative paths based on the route hierarchy:Relative Routing Types
React Router supports two types of relative routing:Layout Routes
Layout routes don’t have a path and exist purely for UI organization:Route Ranking
When multiple routes could match, React Router uses a ranking algorithm:- Static segments (highest priority) -
/about - Dynamic segments -
/:id - Splat routes (lowest priority) -
/*
Basename
The basename allows you to mount your app at a subdirectory:lib/components.tsx:
Router Types
React Router provides different router implementations:BrowserRouter
Uses HTML5 history API for clean URLs:MemoryRouter
Stores history in memory (useful for testing or non-browser environments):lib/components.tsx:311:
Route Objects Structure
Route objects follow theRouteObject interface:
Best Practices
- Use nested routes to share layouts and reduce duplication
- Keep routes flat when nesting isn’t needed for clarity
- Use index routes for default content at a path
- Order matters less - the router ranks by specificity automatically
- Use relative navigation to make components more portable
- Leverage layout routes for shared UI without affecting URLs