UNPKG

react-custom-hooks-utils

Version:

This library contains a collection of reusable React custom hooks to simplify state management, side effects, and user interactions.

20 lines (15 loc) 423 B
import { useEffect, useRef } from 'react'; function useInterval(callback, delay) { const savedCallback = useRef(); useEffect(() => { savedCallback.current = callback; }, [callback]); useEffect(() => { const tick = () => savedCallback.current(); if (delay !== null) { const id = setInterval(tick, delay); return () => clearInterval(id); } }, [delay]); } export default useInterval;