Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

disabling-queries.md

56 lines1.4 KB

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 === 'idle'` or `isIdle` state.
14- The query will not automatically fetch on mount.
15- The query will not automatically refetch in the background when new instances mount or new instances appearing
16- The query will ignore query client `invalidateQueries` and `refetchQueries` calls that would normally result in the query refetching.
17- `refetch` can be used to manually trigger the query to fetch.
18​
19```js
20function Todos() {
21 const {
22 isIdle,
23 isLoading,
24 isError,
25 data,
26 error,
27 refetch,
28 isFetching,
29 } = useQuery('todos', fetchTodoList, {
30 enabled: false,
31 })
32​
33 return (
34 <>
35 <button onClick={() => refetch()}>Fetch Todos</button>
36​
37 {isIdle ? (
38 'Not ready...'
39 ) : isLoading ? (
40 <span>Loading...</span>
41 ) : isError ? (
42 <span>Error: {error.message}</span>
43 ) : (
44 <>
45 <ul>
46 {data.map(todo => (
47 <li key={todo.id}>{todo.title}</li>
48 ))}
49 </ul>
50 <div>{isFetching ? 'Fetching...' : null}</div>
51 </>
52 )}
53 </>
54 )
56```