Loaders
Loaders provide data to route components before they render. They run on the server during SSR and on the client during client-side navigation.Basic Loader
Loaders are async functions that return data for your route:filename=app/routes/product.tsx
Using useLoaderData
Access loader data in your component with the useLoaderData hook:
Loader Arguments
Loaders receive an object with these properties:Returning Responses
Loaders can return various types of data:Plain Objects
Response Objects
Redirects
Error Responses
Parallel Data Loading
Loaders for all matching routes run in parallel:TypeScript
Use the generatedRoute types for full type safety:
Accessing Parent Loader Data
UseuseRouteLoaderData to access data from parent routes:
| Route Filename | Route ID |
|---|---|
app/root.tsx | "root" |
app/routes/teams.tsx | "routes/teams" |
app/routes/teams.$id.tsx | "routes/teams.$id" |
Error Handling
Handle loader errors withErrorBoundary:
Client Loaders
UseclientLoader to load data only on the client:
Best Practices
Keep Loaders Fast
Return Only What You Need
Related
- Actions - Handle mutations
- Fetchers - Load data without navigation
- Revalidation - Keep data fresh