shouldRevalidate
Optimizes data loading by allowing you to skip loader execution when it’s not necessary.Signature
Arguments passed to the shouldRevalidate function
trueto run the loaderfalseto skip the loader and keep existing data
Default Behavior
By default, React Router revalidates (re-runs loaders) in these cases:- After an action is called (form submission)
- When URL search params change
- When a link is clicked to the same URL (user clicks “refresh”)
- When params change for the route
- Navigating to a different route with the same parent
- Params haven’t changed
- No action was called
Basic Example
Skip Revalidation on Specific Actions
Revalidate Based on Action Result
Skip Revalidation on Search Param Changes
Revalidate Only on Specific Param Changes
Time-Based Revalidation
Revalidate on Specific Form Methods
Inspect Action Status
Complex Example
Best Practices
Always consider defaultShouldRevalidate
Always consider defaultShouldRevalidate
Use the default behavior as a fallback to avoid missing important revalidations:
Be conservative with skipping revalidation
Be conservative with skipping revalidation
It’s better to revalidate unnecessarily than to show stale data:
Use for expensive loaders
Use for expensive loaders
Only add shouldRevalidate when loader is computationally expensive:
Test thoroughly
Test thoroughly
Ensure your revalidation logic doesn’t break data updates: