Skip to content

usePolling() ​

usePolling(callback, delay): void

Defined in: hooks/async/usePolling.ts:33

A React hook that executes a callback on a fixed interval. The callback can be updated over time, and polling can be stopped at any time by setting delay to null.

Parameters ​

callback ​

PollingCallback

The function to call on each interval tick.

delay ​

Interval in milliseconds. Set to null to stop polling.

number | null

Returns ​

void

Example ​

tsx
import { useState } from 'react';
import { usePolling } from '@zl-asica/react';

const PollingExample = () => {
  const [count, setCount] = useState(0);

  usePolling(() => {
    setCount((prev) => prev + 1);
  }, 1000); // Poll every 1 second

  return <p>Polling count: {count}</p>;
};

Released under the MIT License.