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
Fromlib/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
Fromlib/router/utils.ts:
Hydrate Fallback
Show loading UI during initial hydration:Meta
Define document metadata:Meta Arguments
Meta Return Types
Links
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
- Export types - Use
typeof loaderfor type inference - Handle errors - Always provide ErrorBoundary
- Validate in actions - Return errors instead of throwing
- Use meta for SEO - Set titles and descriptions
- Lazy load large routes - Improve initial bundle size
- Control revalidation - Optimize with shouldRevalidate
- Keep modules focused - One concern per export
- Use handle for shared data - Breadcrumbs, themes, etc.