Skip to content

Commit

Permalink
Merge pull request #4131 from riqts/skipPollingIfUnfocused-docs
Browse files Browse the repository at this point in the history
[Docs/Website] skipPollingIfUnfocused added to polling overview and query options
  • Loading branch information
EskiMojo14 committed Jan 31, 2024
2 parents d36a3a1 + 5e25dca commit e26d87e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/rtk-query/api/created-api/hooks.mdx
Expand Up @@ -251,6 +251,7 @@ type UseQuery = (

type UseQueryOptions = {
pollingInterval?: number
skipPollingIfUnfocused?: boolean
refetchOnReconnect?: boolean
refetchOnFocus?: boolean
skip?: boolean
Expand Down Expand Up @@ -445,6 +446,7 @@ type UseQuerySubscriptionOptions = {
skip?: boolean
refetchOnMountOrArgChange?: boolean | number
pollingInterval?: number
skipPollingIfUnfocused?: boolean
refetchOnReconnect?: boolean
refetchOnFocus?: boolean
}
Expand Down Expand Up @@ -485,6 +487,7 @@ type UseLazyQuery = (

type UseLazyQueryOptions = {
pollingInterval?: number
skipPollingIfUnfocused?: boolean
refetchOnReconnect?: boolean
refetchOnFocus?: boolean
selectFromResult?: (result: UseQueryStateDefaultResult) => any
Expand Down Expand Up @@ -555,6 +558,7 @@ type UseLazyQuerySubscription = (

type UseLazyQuerySubscriptionOptions = {
pollingInterval?: number
skipPollingIfUnfocused?: boolean
refetchOnReconnect?: boolean
refetchOnFocus?: boolean
}
Expand Down
9 changes: 8 additions & 1 deletion docs/rtk-query/usage/polling.mdx
Expand Up @@ -14,14 +14,21 @@ description: 'RTK Query > Usage > Polling: re-fetching data on a timer'

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:

:::tip
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.

_Note: `skipPollingIfUnfocused` requires [`setupListeners`](../api/setupListeners.mdx) to have been called._
:::

```tsx no-transpile title="src/Pokemon.tsx" no-transpile
import * as React from 'react'
import { useGetPokemonByNameQuery } from './services/pokemon'

export const Pokemon = ({ name }: { name: string }) => {
// Automatically refetch every 3s
// Automatically refetch every 3s unless the window is out of focus
const { data, status, error, refetch } = useGetPokemonByNameQuery(name, {
pollingInterval: 3000,
skipPollingIfUnfocused: true,
})

return <div>{data}</div>
Expand Down

0 comments on commit e26d87e

Please sign in to comment.