@ducor/hooks
Version:
A collection of useful React hooks for building modern web applications. Includes hooks for clipboard operations, window events, intervals, timeouts, and more.
20 lines (19 loc) • 602 B
JavaScript
import { useEffect, useRef } from "react";
/**
* `useInterval` is a custom hook that runs a function at a specified interval.
*
* @see Docs https://ui.ducor.net/hooks/use-interval
*/
const useInterval = (callback, delay) => {
const savedCallback = useRef(callback);
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
useEffect(() => {
if (delay === null)
return;
const intervalId = setInterval(() => savedCallback.current(), delay);
return () => clearInterval(intervalId);
}, [delay]);
};
export default useInterval;