mango-v4-ui/components/shared/useInterval.tsx

27 lines
567 B
TypeScript
Raw Normal View History

import { useRef, useEffect } from 'react'
export default function useInterval(
callback: (...args: any[]) => any,
delay: number
) {
const savedCallback = useRef<() => void>()
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback
}, [callback])
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current && savedCallback.current()
}
if (delay !== null) {
const id = setInterval(tick, delay)
return () => {
clearInterval(id)
}
}
}, [delay])
}