import { useEffect , useRef } from 'react' ; type Delay = number | null ; type TimerHandler = ( ... args : any [ ] ) => void ; param callback param delay const useInterval = ( callback , delay ) => { const savedCallbackRef = useRef < TimerHandler > ( ) ; useEffect ( ( ) => { savedCallbackRef . current = callback ; } , [ callback ] ) ; useEffect ( ( ) => { const handler = ( ... args ) => savedCallbackRef . current ! ( ... args ) ; if ( delay !== null ) { const intervalId = setInterval ( handler , delay ) ; return ( ) => clearInterval ( intervalId ) ; } } , [ delay ] ) ; } ; export default useInterval ;