1 // https://usehooks-ts.com/react-hook/use-interval
2 // See also https://overreacted.io/making-setinterval-declarative-with-react-hooks/
3 import { useEffect, useRef } from 'react';
5 import noop from '@proton/utils/noop';
7 const useInterval = (callback: () => void, delay: number | null) => {
8 const savedCallback = useRef<() => void>(noop);
10 // Remember the latest callback.
12 savedCallback.current = callback;
15 // Set up the interval.
21 const id = setInterval(() => savedCallback.current(), delay);
23 return () => clearInterval(id);
27 export default useInterval;