Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs/Website] skipPollingIfUnfocused added to polling overview and query options #4131

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
riqts marked this conversation as resolved.
Show resolved Hide resolved

_Note: `skipPollingIfUnfocused` requires [`setupListeners`](../api/setupListeners.mdx) to have been called and is disabled by default_
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
:::

```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