Skip to main content

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.
With the layout component:

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 the matchRoutes function from lib/router/utils.ts. The algorithm:
  1. Normalizes the pathname - Removes basename and decodes URI components
  2. Ranks routes - Routes are ranked by specificity (static > dynamic > splat)
  3. Matches segments - Each URL segment is matched against route patterns
  4. 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:
  1. Static segments (highest priority) - /about
  2. Dynamic segments - /:id
  3. Splat routes (lowest priority) - /*

Basename

The basename allows you to mount your app at a subdirectory:
Implementation from 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):
From lib/components.tsx:311:

Route Objects Structure

Route objects follow the RouteObject interface:

Best Practices

  1. Use nested routes to share layouts and reduce duplication
  2. Keep routes flat when nesting isn’t needed for clarity
  3. Use index routes for default content at a path
  4. Order matters less - the router ranks by specificity automatically
  5. Use relative navigation to make components more portable
  6. Leverage layout routes for shared UI without affecting URLs