Open History to see the last commit
1---
2id: background-fetching-indicators
3title: Background Fetching Indicators
4---
5β
6A query's `status === 'pending'` state is sufficient enough to show the initial hard-loading state for a query, but sometimes you may want to display an additional indicator that a query is refetching in the background. To do this, queries also supply you with an `isFetching` boolean that you can use to show that it's in a fetching state, regardless of the state of the `status` variable:
7β
8[//]: # 'Example'
9β
10```tsx
11function Todos() {
12 const {
13 status,
14 data: todos,
15 error,
16 isFetching,
17 } = useQuery({
18 queryKey: ['todos'],
19 queryFn: fetchTodos,
20 })
21β
22 return status === 'pending' ? (
23 <span>Loading...</span>
24 ) : status === 'error' ? (
25 <span>Error: {error.message}</span>
26 ) : (
27 <>
28 {isFetching ? <div>Refreshing...</div> : null}
29β
30 <div>
31 {todos.map((todo) => (
32 <Todo todo={todo} />
33 ))}
34 </div>
35 </>
36 )
37}
38```
39β
40[//]: # 'Example'
41β
42## Displaying Global Background Fetching Loading State
43β
44In addition to individual query loading states, if you would like to show a global loading indicator when **any** queries are fetching (including in the background), you can use the `useIsFetching` hook:
45β
46[//]: # 'Example2'
47β
48```tsx
49import { useIsFetching } from '@tanstack/react-query'
50β
51function GlobalLoadingIndicator() {
52 const isFetching = useIsFetching()
53β
54 return isFetching ? (
55 <div>Queries are fetching in the background...</div>
56 ) : null
57}
58```
59β
60[//]: # 'Example2'