Capítulo 19 de 80
refetchInterval refetches a query on a timer independent of staleTime, on a per-function basis when passed a function instead of a number (letting the query's own state decide whether/how fast to keep polling).
refetchInterval: 5_000 refetches every 5s while there's at least one active observer, regardless of freshness — polling and staleness are independent mechanisms.(query) => number | false — return false to stop polling (e.g. once a job's status is 'complete'), a number to keep going at that cadence; polling resumes automatically if the function would return a number again.refetchIntervalInBackground: true to keep polling in inactive tabs (dashboards, live data).useQuery runs its own timer (per-observer), but concurrent in-flight fetches for the same key are still deduplicated to one network request — timers are per-observer, network dedup is per-query.online/offline browser events don't fire (Electron, some WebViews), set networkMode: 'always' to skip the connectivity gate entirely for a polling query.// Stop polling once a job finishes
useQuery({
queryKey: ['job', jobId],
queryFn: () => fetchJobStatus(jobId),
refetchInterval: (query) => (query.state.data?.status === 'complete' ? false : 2_000),
})
// Keep polling even when the tab is in the background
useQuery({
queryKey: ['portfolio'],
queryFn: fetchPortfolio,
refetchInterval: 30_000,
refetchIntervalInBackground: true,
})
refetchInterval whenever polling should stop on its own (job done, error state) — it's the built-in way to avoid a manual clearInterval dance.networkMode: 'always' for polling queries in non-browser or unreliable-connectivity-event environments (Electron, WebViews), not just React Native.networkMode: 'always' used here for unreliable connectivity detection.refetchInterval relates to (and is independent of) staleTime.