2022-03-02 17:13:46 +01:00
|
|
|
import { useRef, useEffect } from 'react';
|
|
|
|
|
|
|
|
export default function useInterval(callback, delay) {
|
|
|
|
const savedCallback = useRef();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
savedCallback.current = callback;
|
|
|
|
}, [callback]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const tick = () => {
|
|
|
|
savedCallback.current();
|
|
|
|
};
|
|
|
|
if (delay !== null) {
|
2022-03-03 03:25:44 +01:00
|
|
|
const id = setInterval(tick, delay);
|
2022-03-02 17:13:46 +01:00
|
|
|
return () => clearInterval(id);
|
|
|
|
}
|
|
|
|
}, [delay]);
|
|
|
|
}
|