loader
A server-side function that loads data for a route before it renders.Signature
Arguments passed to the loader function
Can return:
- A Response object (with
json(),redirect(), etc.) - Plain data (automatically serialized)
- A Promise resolving to either
Basic Example
Returning Responses
Reading Request Data
Using Context
Best Practices
Type safety with Route.LoaderArgs
Type safety with Route.LoaderArgs
Use the route-specific type for automatic param type inference:
Error handling
Error handling
Throw responses or errors to trigger error boundaries:
Avoid serialization issues
Avoid serialization issues
Only return JSON-serializable data:
Parallel data loading
Parallel data loading
Use
Promise.all() to load data in parallel:See Also
- clientLoader - Client-side data loading
- useLoaderData - Access loader data in components
- defer - Stream data to the client