import { useRef, useEffect } from 'react'; export const usePrevious = (value) => { const ref = useRef(); useEffect(() => { ref.current = value; }); return ref.current; }; export const useEffectAsync = (effect, deps) => { const ref = useRef(); useEffect(() => { effect().then((result) => ref.current = result); return () => { const result = ref.current; if (result) { result(); } }; }, deps); }; // Source: https://overreacted.io/making-setinterval-declarative-with-react-hooks/ export const useInterval = (callback, delay) => { const ref = useRef(); let id = null; // Remember the latest callback. useEffect(() => { ref.current = callback; }, [callback]); // Set up the interval. useEffect(() => { function tick() { ref.current(); } if (delay !== null) { id = setInterval(tick, delay); return () => clearInterval(id); } }, [delay]); return id; }