Skip to content

usePolling()

usePolling(callback, delay): void

Defined in: src/hooks/async/usePolling.ts:26

A custom React hook to execute a callback function at a fixed interval. Supports dynamically updating the callback or stopping the polling by setting the delay to null.

Parameters

callback

() => void

The function to execute at each interval.

delay

The interval in milliseconds. Set to null to stop polling.

null | number

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.