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

background-fetching-indicators.md

47 lines1.4 KB

1---
2id: background-fetching-indicators
3title: Background Fetching Indicators
4---
5​
6A query's `status === 'loading'` 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```tsx
9function Todos() {
10 const { status, data: todos, error, isFetching } = useQuery(
11 ['todos'],
12 fetchTodos
13 )
14​
15 return status === 'loading' ? (
16 <span>Loading...</span>
17 ) : status === 'error' ? (
18 <span>Error: {error.message}</span>
19 ) : (
20 <>
21 {isFetching ? <div>Refreshing...</div> : null}
22​
23 <div>
24 {todos.map(todo => (
25 <Todo todo={todo} />
26 ))}
27 </div>
28 </>
29 )
31```
32​
33# Displaying Global Background Fetching Loading State
34​
35In 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:
36​
37```tsx
38import { useIsFetching } from '@tanstack/react-query'
39​
40function GlobalLoadingIndicator() {
41 const isFetching = useIsFetching()
42​
43 return isFetching ? (
44 <div>Queries are fetching in the background...</div>
45 ) : null
47```