A React hook that sets up an interval to run a callback function at the specified delay.

This hook schedules the provided callback function to be invoked repeatedly after the specified delay (in milliseconds). If the delay is null, the interval is paused.

import { useState } from "react";
import { useInterval } from "essential-hooks-collection";

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

useInterval(() => {
setCount(prevCount => prevCount + 1);
}, 1000);

return <div>{count}</div>;
};

export default MyComponent;
  • Parameters

    • callback: () => void

      The callback function to execute on each interval tick.

    • delay: null | number

      The delay in milliseconds between ticks. Pass null to pause the interval.

    Returns void