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, then the query will be initialized in the `status === 'success'` or `isSuccess` state.
11- If the query does not have cached data, then the query will start in the `status === 'pending'` and `fetchStatus === 'idle'` state.
12- The query will not automatically fetch on mount.
13- The query will not automatically refetch in the background.
14- The query will ignore query client `invalidateQueries` and `refetchQueries` calls that would normally result in the query refetching.
15- `refetch` returned from `useQuery` can be used to manually trigger the query to fetch.
16β
17[//]: # 'Example'
18β
19```tsx
20function Todos() {
21 const { isLoading, isError, data, error, refetch, isFetching } =
22 useQuery({
23 queryKey: ['todos'],
24 queryFn: fetchTodoList,
25 enabled: false,
26 })
27β
28 return (
29 <div>
30 <button onClick={() => refetch()}>Fetch Todos</button>
31β
32 {data ? (
33 <>
34 <ul>
35 {data.map((todo) => (
36 <li key={todo.id}>{todo.title}</li>
37 ))}
38 </ul>
39 </>
40 ) : isError ? (
41 <span>Error: {error.message}</span>
42 ) : isLoading ? (
43 <span>Loading...</span>
44 ) : (
45 <span>Not ready ...</span>
46 )}
47β
48 <div>{isFetching ? 'Fetching...' : null}</div>
49 </div>
50 )
51}
52```
53β
54[//]: # 'Example'
55β
56Permanently disabling a query opts out of many great features that TanStack 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:
57β
58## Lazy Queries
59β
60The 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:
61β
62[//]: # 'Example2'
63β
64```tsx
65function Todos() {
66 const [filter, setFilter] = React.useState('')
67β
68 const { data } = useQuery({
69 queryKey: ['todos', filter],
70 queryFn: () => fetchTodos(filter),
71 // β¬οΈ disabled as long as the filter is empty
72 enabled: !!filter
73 })
74β
75 return (
76 <div>
77 // π applying the filter will enable and execute the query
78 <FiltersForm onApply={setFilter} />
79 {data && <TodosTable data={data}} />
80 </div>
81 )
82}
83```
84β
85[//]: # 'Example2'
86β
87### isLoading (Previously: `isInitialLoading`)
88β
89Lazy queries will be in `status: 'pending'` right from the start because `pending` 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.
90β
91If you are using disabled or lazy queries, you can use the `isLoading` flag instead. It's a derived flag that is computed from:
92β
93`isPending && isFetching`
94β
95so it will only be true if the query is currently fetching for the first time.