Skip to main content
Disabling queries allows you to control precisely when queries should run. This is essential for optimizing performance, handling user interactions, and managing dependent data flows.

Using the enabled Option

The enabled 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 with enabled: 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:
Combine multiple conditions in the enabled option to ensure all prerequisites are met before executing a query.

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:
If you want to cache data across tab switches, set enabled: true and use staleTime instead. Disabled queries won’t fetch data even if it’s stale.

Permanently Disabled Queries

Some queries should never run automatically:

Disabling During Mutations

Disable queries while mutations are in progress:
Disabling queries during editing prevents race conditions where background refetches could overwrite user changes.

Query Status with Disabled

Understand how query status works with enabled: 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 when enabled changes from false to true:

Refetch with enabled: false

You can manually refetch even when enabled is false:
Use refetch() for one-time manual fetches and enabled for controlling automatic fetching behavior (on mount, window focus, network reconnect, etc.).

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.