Using the enabled Option
Theenabled option controls whether a query should automatically run:
When
enabled is false, the query will stay in an idle status and won’t execute until enabled becomes true.Lazy Queries
Implement lazy loading by starting withenabled: false and manually triggering the query:
1
Set enabled to false
Prevent the query from running automatically on mount.
2
Trigger with refetch
Call
refetch() manually when you want to execute the query.3
Handle loading states
Use
isFetching to show loading indicators during manual fetches.Conditional Query Execution
Enable queries based on complex conditions:Function-Based enabled
Use a function for dynamic enable logic:User-Triggered Queries
Fetch data only when the user explicitly requests it:This pattern is useful for expensive queries or data that’s not needed immediately, reducing unnecessary API calls and improving initial load performance.
Tab-Based Query Activation
Load data only for the active tab:Permanently Disabled Queries
Some queries should never run automatically:Disabling During Mutations
Disable queries while mutations are in progress:Query Status with Disabled
Understand how query status works withenabled: false:
A disabled query can have
status: 'success' if it has cached data from a previous fetch, but fetchStatus will be 'idle' indicating it’s not currently fetching.Re-enabling Queries
Queries automatically run whenenabled changes from false to true:
Refetch with enabled: false
You can manually refetch even whenenabled is false:
Combining with Other Options
Disabled queries work alongside other query options:Debugging Disabled Queries
Use the devtools to inspect disabled queries:In the devtools, disabled queries show with a “disabled” badge, helping you identify why a query isn’t fetching.