import { useEffect , useRef } from 'react' ; import { isNotUndefined } from '../utils/array' ; type Callback = ( ) => void ; const useInterval = ( callback , delay ) => { const savedCallback = useRef < Callback > ( ) ; useEffect ( ( ) => { savedCallback . current = callback ; } , [ callback ] ) ; useEffect ( ( ) => { function tick ( ) { if ( savedCallback . current ) { savedCallback . current ! ( ) ; } } if ( isNotUndefined ( delay ) ) { const id = setInterval ( tick , delay ) ; return ( ) => clearInterval ( id ) ; } } , [ delay ] ) ; } ; export default useInterval ;