import { useEffect, useRef } from 'react'; export function useInterval(callback: Function, delay: number) { const callbacRef = useRef(); // update callback function with current render callback that has access to latest props and state useEffect(() => { callbacRef.current = callback; }); useEffect(() => { if (!delay) { return () => {}; } const interval = setInterval(() => { callbacRef.current && callbacRef.current(); }, delay); return () => clearInterval(interval); }, [delay]); }