UNPKG

react-snackbar-ui-customizable

Version:
25 lines (18 loc) 570 B
import { useEffect, useRef } from 'react' const useInterval = (callback: () => void, delay: number | null): void => { const savedCallback = useRef(callback) // Remember the latest callback if it changes. useEffect(() => { savedCallback.current = callback }, [callback]) // Set up the interval. useEffect(() => { // Don't schedule if no delay is specified. if (delay === null) { return } const id = setInterval(() => savedCallback.current(), delay) return () => clearInterval(id) }, [delay]) } export default useInterval