TanStack Query is configured with aggressive but sensible defaults. Understanding these defaults will help you build better applications and avoid common pitfalls.
Default Query Behavior
By default, TanStack Query assumes you want fresh data and will aggressively refetch to keep your UI synchronized with the server.
Stale Time: 0ms
Queries are immediately considered stale after fetching completes.
Stale queries are refetched automatically in the background when:
- New instances of the query mount
- The window is refocused
- The network is reconnected
- Configured refetch intervals trigger
Garbage Collection Time: 5 Minutes
Inactive queries are garbage collected after 5 minutes (300,000ms).
If users navigate back to a page before gcTime expires, they’ll see cached data instantly. After gcTime expires, there will be a loading state.
Automatic Refetching Defaults
Refetch on Window Focus: true
Queries automatically refetch when the window regains focus if the data is stale.
Refetch on Mount: true
Queries refetch on mount if the data is stale.
Refetch on Reconnect: true
Queries refetch when the network reconnects if the data is stale.
Retry Defaults
Retry: 3 Attempts
Failed queries retry 3 times with exponential backoff before showing an error.
Retry Delay: Exponential Backoff
Retries use exponential backoff by default:
Setting Global Defaults
Override defaults globally for all queries:
Set sensible defaults globally, then override them for specific queries that need different behavior.
Network Mode Defaults
Network Mode: online
Queries and mutations only run when there’s an active network connection.
Query Enabled Default
Queries are enabled by default and run automatically.
Recommended Configurations
For Static/Slow-Changing Data
For Real-Time Data
For User-Specific Data
For List/Detail Pattern
Changing defaults can significantly impact your app’s behavior. Always test thoroughly when adjusting these settings.