Skip to content

Commit e26d87e

Browse files
authored
Merge pull request #4131 from riqts/skipPollingIfUnfocused-docs
[Docs/Website] skipPollingIfUnfocused added to polling overview and query options
2 parents d36a3a1 + 5e25dca commit e26d87e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ type UseQuery = (
251251

252252
type UseQueryOptions = {
253253
pollingInterval?: number
254+
skipPollingIfUnfocused?: boolean
254255
refetchOnReconnect?: boolean
255256
refetchOnFocus?: boolean
256257
skip?: boolean
@@ -445,6 +446,7 @@ type UseQuerySubscriptionOptions = {
445446
skip?: boolean
446447
refetchOnMountOrArgChange?: boolean | number
447448
pollingInterval?: number
449+
skipPollingIfUnfocused?: boolean
448450
refetchOnReconnect?: boolean
449451
refetchOnFocus?: boolean
450452
}
@@ -485,6 +487,7 @@ type UseLazyQuery = (
485487

486488
type UseLazyQueryOptions = {
487489
pollingInterval?: number
490+
skipPollingIfUnfocused?: boolean
488491
refetchOnReconnect?: boolean
489492
refetchOnFocus?: boolean
490493
selectFromResult?: (result: UseQueryStateDefaultResult) => any
@@ -555,6 +558,7 @@ type UseLazyQuerySubscription = (
555558

556559
type UseLazyQuerySubscriptionOptions = {
557560
pollingInterval?: number
561+
skipPollingIfUnfocused?: boolean
558562
refetchOnReconnect?: boolean
559563
refetchOnFocus?: boolean
560564
}

docs/rtk-query/usage/polling.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ description: 'RTK Query > Usage > Polling: re-fetching data on a timer'
1414

1515
Polling gives you the ability to have a 'real-time' effect by causing a query to run at a specified interval. To enable polling for a query, pass a `pollingInterval` to the `useQuery` hook or action creator with an interval in milliseconds:
1616

17+
:::tip
18+
Polling additionally has the ability to skip sending requests while the window is out of focus. To enable this behavior, pass `skipPollingIfUnfocused: true` to the `useQuery` hook or action creator.
19+
20+
_Note: `skipPollingIfUnfocused` requires [`setupListeners`](../api/setupListeners.mdx) to have been called._
21+
:::
22+
1723
```tsx no-transpile title="src/Pokemon.tsx" no-transpile
1824
import * as React from 'react'
1925
import { useGetPokemonByNameQuery } from './services/pokemon'
2026

2127
export const Pokemon = ({ name }: { name: string }) => {
22-
// Automatically refetch every 3s
28+
// Automatically refetch every 3s unless the window is out of focus
2329
const { data, status, error, refetch } = useGetPokemonByNameQuery(name, {
2430
pollingInterval: 3000,
31+
skipPollingIfUnfocused: true,
2532
})
2633

2734
return <div>{data}</div>

0 commit comments

Comments
 (0)