|
| 1 | +--- |
| 2 | +id: polling |
| 3 | +title: Polling |
| 4 | +--- |
| 5 | + |
| 6 | +`refetchInterval` makes a query refetch on a timer. Set it to a number in milliseconds and the query runs every N ms while there's at least one active observer: |
| 7 | + |
| 8 | +[//]: # 'Example1' |
| 9 | + |
| 10 | +```tsx |
| 11 | +useQuery({ |
| 12 | + queryKey: ['prices'], |
| 13 | + queryFn: fetchPrices, |
| 14 | + refetchInterval: 5_000, // every 5 seconds |
| 15 | +}) |
| 16 | +``` |
| 17 | + |
| 18 | +[//]: # 'Example1' |
| 19 | + |
| 20 | +Polling is independent of `staleTime`. A query can be fresh and still poll on schedule; see [Important Defaults](./important-defaults.md) for how `staleTime` interacts with other refetch behaviors. `refetchInterval` fires on its own clock regardless of freshness. |
| 21 | + |
| 22 | +## Adapting the interval to query state |
| 23 | + |
| 24 | +Pass a function instead of a number to compute the interval from the current query. The function receives the `Query` object and should return a number in ms or `false` to stop polling: |
| 25 | + |
| 26 | +[//]: # 'Example2' |
| 27 | + |
| 28 | +```tsx |
| 29 | +useQuery({ |
| 30 | + queryKey: ['job', jobId], |
| 31 | + queryFn: () => fetchJobStatus(jobId), |
| 32 | + refetchInterval: (query) => { |
| 33 | + // Stop polling once the job finishes |
| 34 | + if (query.state.data?.status === 'complete') return false |
| 35 | + return 2_000 |
| 36 | + }, |
| 37 | +}) |
| 38 | +``` |
| 39 | + |
| 40 | +[//]: # 'Example2' |
| 41 | + |
| 42 | +Returning `false` clears the interval timer. If the query result changes so the function would return a positive number again, polling resumes automatically. |
| 43 | + |
| 44 | +## Background polling |
| 45 | + |
| 46 | +By default, polling pauses when the browser tab loses focus. For dashboards or any interface where data needs to stay current even while the user is in another tab, disable that behavior: |
| 47 | + |
| 48 | +[//]: # 'Example3' |
| 49 | + |
| 50 | +```tsx |
| 51 | +useQuery({ |
| 52 | + queryKey: ['portfolio'], |
| 53 | + queryFn: fetchPortfolio, |
| 54 | + refetchInterval: 30_000, |
| 55 | + refetchIntervalInBackground: true, |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +[//]: # 'Example3' |
| 60 | + |
| 61 | +## Pausing polling |
| 62 | + |
| 63 | +Pass a function to `refetchInterval` and close over component state to control when polling runs: |
| 64 | + |
| 65 | +[//]: # 'Example4' |
| 66 | + |
| 67 | +```tsx |
| 68 | +useQuery({ |
| 69 | + queryKey: ['prices', tokenAddress], |
| 70 | + queryFn: () => fetchPrice(tokenAddress), |
| 71 | + refetchInterval: () => { |
| 72 | + if (!tokenAddress || isPaused) return false |
| 73 | + return 15_000 |
| 74 | + }, |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +[//]: # 'Example4' |
| 79 | + |
| 80 | +## Polling with offline support |
| 81 | + |
| 82 | +TanStack Query detects connectivity by listening to the browser's `online` and `offline` events. In environments where those events don't fire reliably (Electron, some embedded WebViews), set `networkMode: 'always'` to skip the connectivity check: |
| 83 | + |
| 84 | +[//]: # 'Example5' |
| 85 | + |
| 86 | +```tsx |
| 87 | +useQuery({ |
| 88 | + queryKey: ['chainStatus'], |
| 89 | + queryFn: fetchChainStatus, |
| 90 | + refetchInterval: 10_000, |
| 91 | + networkMode: 'always', |
| 92 | +}) |
| 93 | +``` |
| 94 | + |
| 95 | +[//]: # 'Example5' |
| 96 | + |
| 97 | +For more on network modes, see [Network Mode](./network-mode.md). |
| 98 | + |
| 99 | +## Note on deduplication |
| 100 | + |
| 101 | +Each `QueryObserver` (each component using `useQuery` with `refetchInterval`) runs its own timer. Two components subscribed to the same key with `refetchInterval: 5000` each fire their timer every 5 seconds. What gets deduplicated is concurrent in-flight fetches: if two timers fire at the same time, only one network request goes out. The timers are observer-level; the deduplication is query-level. |
| 102 | + |
| 103 | +[//]: # 'ReactNative' |
| 104 | + |
| 105 | +## Non-browser environments |
| 106 | + |
| 107 | +For non-browser runtimes like React Native, the standard `online`/`offline` and focus events aren't available. The [React Native guide](../react-native.md) covers how to connect `focusManager` and `onlineManager` to native app state APIs. |
| 108 | + |
| 109 | +[//]: # 'ReactNative' |
0 commit comments