import { useEffect , useRef } from 'react' ; type Callback = ( ) => void ; type UseIntervalOptions = { delay : number ; pause : boolean ; } ; export const useInterval = ( callback , { delay , pause = false } ) : void => { const savedCallback = useRef < Callback > ( ) ; useEffect ( ( ) => { savedCallback . current = callback ; } , [ callback ] ) ; useEffect ( ( ) => { const handler = ( ) => savedCallback . current && savedCallback . current ( ) ; if ( ! pause ) { const id = setInterval ( handler , delay ) ; return ( ) => clearInterval ( id ) ; } } , [ delay , pause ] ) ; } ;