Open History to see the last commit
1---
2id: disabling-queries
3title: Disabling/Pausing Queries
4---
5β
6If you ever want to disable a query from automatically running, you can use the `enabled = false` option.
7β
8When `enabled` is `false`:
9β
10- If the query has cached data
11 - The query will be initialized in the `status === 'success'` or `isSuccess` state.
12- If the query does not have cached data
13 - The query will start in the `status === 'loading'` and `fetchStatus === 'idle'`
14- The query will not automatically fetch on mount.
15- The query will not automatically refetch in the background
16- The query will ignore query client `invalidateQueries` and `refetchQueries` calls that would normally result in the query refetching.
17- `refetch` returned from `useQuery` can be used to manually trigger the query to fetch.
18β
19```tsx
20function Todos() {
21 const {
22 isInitialLoading,
23 isError,
24 data,
25 error,
26 refetch,
27 isFetching
28 } = useQuery(['todos'], fetchTodoList, {
29 enabled: false,
30 })
31β
32 return (
33 <div>
34 <button onClick={() => refetch()}>Fetch Todos</button>
35β
36 {data ? (
37 <>
38 <ul>
39 {data.map(todo => (
40 <li key={todo.id}>{todo.title}</li>
41 ))}
42 </ul>
43 </>
44 ) : (
45 isError ? (
46 <span>Error: {error.message}</span>
47 ) : (
48 isInitialLoading ? (
49 <span>Loading...</span>
50 ) : (
51 <span>Not ready ...</span>
52 )
53 )
54 )}
55β
56 <div>{isFetching ? 'Fetching...' : null}</div>
57 </div>
58 )
59}
60```
61β
62Permanently disabling a query opts out of many great features that react-query has to offer (like background refetches), and it's also not the idiomatic way. It takes you from the declarative approach (defining dependencies when your query should run) into an imperative mode (fetch whenever I click here). It is also not possible to pass parameters to `refetch`. Oftentimes, all you want is a lazy query that defers the initial fetch:
63β
64## Lazy Queries
65β
66The enabled option can not only be used to permanently disable a query, but also to enable / disable it at a later time. A good example would be a filter form where you only want to fire off the first request once the user has entered a filter value:
67β
68```tsx
69function Todos() {
70 const [filter, setFilter] = React.useState('')
71β
72 const { data } = useQuery(
73 ['todos', filter],
74 () => fetchTodos(filter),
75 {
76 // β¬οΈ disabled as long as the filter is empty
77 enabled: !!filter
78 }
79 )
80β
81 return (
82 <div>
83 // π applying the filter will enable and execute the query
84 <FiltersForm onApply={setFilter} />
85 {data && <TodosTable data={data}} />
86 </div>
87 )
88}
89```
90β
91### isInitialLoading
92β
93Lazy queries will be in `status: 'loading'` right from the start because `loading` means that there is no data yet. This is technically true, however, since we are not currently fetching any data (as the query is not _enabled_), it also means you likely cannot use this flag to show a loading spinner.
94β
95If you are using disabled or lazy queries, you can use the `isInitialLoading` flag instead. It's a derived flag that is computed from:
96β
97`isLoading && isFetching`
98β
99so it will only be true if the query is currently fetching for the first time.