Skip to main content

Route Module API

In Framework mode, route modules are files that export specific functions and components that define your route’s behavior. React Router calls these exports at the right time to load data, handle mutations, and render UI.

Route Module Structure

From lib/types/route-module.ts, a route module can export:

Component Export

The default export is your route’s component:

Using Loader Data

Loader

Loaders run before the route renders:

Loader Arguments

Loader Returns

Action

Actions handle form submissions and mutations:

Using Action Data

Client Loader

Runs on the client for SPA-style data loading:

Hydration

Client Action

Handle mutations on the client:

Error Boundary

Handle errors thrown from loaders, actions, or render:

Error Types

From lib/router/utils.ts:

Hydrate Fallback

Show loading UI during initial hydration:

Meta

Define document metadata:

Meta Arguments

Meta Return Types

Define link tags for stylesheets, preloads, etc.:

Headers

Set HTTP response headers (SSR only):

Headers Arguments

Handle

Attach custom data to routes:

Should Revalidate

Control when loaders re-run:

Lazy Loading

Code-split routes:

Module Execution Flow

Complete Example

Best Practices

  1. Export types - Use typeof loader for type inference
  2. Handle errors - Always provide ErrorBoundary
  3. Validate in actions - Return errors instead of throwing
  4. Use meta for SEO - Set titles and descriptions
  5. Lazy load large routes - Improve initial bundle size
  6. Control revalidation - Optimize with shouldRevalidate
  7. Keep modules focused - One concern per export
  8. Use handle for shared data - Breadcrumbs, themes, etc.