Data Fetching Patterns
React Router provides powerful data fetching primitives that eliminate loading states, race conditions, and enable advanced patterns like parallel loading, deferred data, and optimistic UI.Core Data Fetching
Route Loaders
Loaders are the primary way to fetch data:Parallel Loading
Nested routes load data in parallel automatically:Sequential Loading
When you need data from a parent:Fetchers
Fetchers let you load data without navigation:Fetcher States
Fromlib/router/router.ts, fetcher states:
Fetcher Examples
Deferred Data
Stream slow data for better UX:When to Defer
Revalidation
Data automatically refreshes after mutations:Manual Revalidation
Controlling Revalidation
Optimistic UI
Update UI immediately, then confirm:Client-Side Caching
Implement caching with client loaders:Error Handling
Loader Errors
Fetcher Errors
Data Access Patterns
Parent Data
All Route Data
Advanced Patterns
Prefetching
Infinite Scroll
Polling
Data Flow Diagram
Best Practices
- Load in parallel - Use nested routes for automatic parallelization
- Defer slow data - Keep pages interactive with Suspense
- Use fetchers for interactions - Add to cart, likes, etc.
- Implement optimistic UI - Better perceived performance
- Handle errors gracefully - Provide meaningful error boundaries
- Cache strategically - Use client loaders for expensive data
- Revalidate intelligently - Control with shouldRevalidate
- Prefetch on hover - Improve navigation perceived performance