Polling
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:
src/Pokemon.tsx
import * as React from 'react';
import { useGetPokemonByNameQuery } from './services/pokemon';
export const Pokemon = ({ name }: { name: string }) => {
// Automatically refetch every 3s
const { data, status, error, refetch } = useGetPokemonByNameQuery(name, { pollingInterval: 3000 });
return <div>{data}</div>;
};
In an action creator without React Hooks:
const { data, status, error, refetch } = store.dispatch(
endpoints.getCountById.initiate(id, { subscriptionOptions: { pollingInterval: 3000 } })
);
Usage without React Hooks
If you use polling without the convenience of React Hooks, you will need to manually call updateSubscriptionOptions
on the promise ref to update the interval. This approach varies by framework but is possible everywhere. See the Svelte Example for one possibility.
queryRef.updateSubscriptionOptions({ pollingInterval: 0 });