react-healthy
Version:
Incrementally check and alert your users to the health of your APIs
24 lines (19 loc) • 533 B
text/typescript
import {useEffect, useRef} from 'react'
const useInterval = (callback: () => void, delay: number) => {
const savedCallback = useRef<() => void | undefined>()
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback
}, [callback])
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current?.()
}
if (delay !== null) {
const id = setInterval(tick, delay)
return () => clearInterval(id)
}
}, [delay])
}
export default useInterval