import { useEffect , useRef } from 'react' ; export const useInterval = ( callback , delay ) => { const savedCallback = useRef ( ( ) => { } ) ; useEffect ( ( ) => { savedCallback . current = callback ; } ) ; useEffect ( ( ) => { const listener = setInterval ( savedCallback . current , delay ) ; return ( ) => clearInterval ( listener ) ; } , [ delay ] ) ; } ; export const useTimeout = ( callback , delay ) => { const savedCallback = useRef ( ( ) => { } ) ; useEffect ( ( ) => { savedCallback . current = callback ; } ) ; useEffect ( ( ) => { const tick = ( ) => { savedCallback . current ( ) ; } ; const listener = setTimeout ( tick , delay ) ; return ( ) => clearTimeout ( listener ) ; } , [ delay ] ) ; } ; export const useTimeoutWithUnmount = ( callback , clearCallback , delay ) => { const savedCallback = useRef ( ( ) => { } ) ; const savedClearCallback = useRef ( ( ) => { } ) ; useEffect ( ( ) => { savedCallback . current = callback ; savedClearCallback . current = clearCallback ; } ) ; useEffect ( ( ) => { const tick = ( ) => { savedCallback . current ( ) ; } ; const listener = setTimeout ( tick , delay ) ; return ( ) => { clearTimeout ( listener ) ; savedClearCallback . current ( ) ; } ; } , [ delay ] ) ; } ; export default { useInterval , useTimeout , useTimeoutWithUnmount , } ;