Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / hooks / useInterval.ts
blob5b0c492dcb85725037012dc24303ca352d282bc6
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.
11     useEffect(() => {
12         savedCallback.current = callback;
13     }, [callback]);
15     // Set up the interval.
16     useEffect(() => {
17         if (delay === null) {
18             return;
19         }
21         const id = setInterval(() => savedCallback.current(), delay);
23         return () => clearInterval(id);
24     }, [delay]);
27 export default useInterval;