Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created November 4, 2023 17:34
Show Gist options
  • Save GalindoSVQ/faeeeb2ab46b5860604a5173e08fa74e to your computer and use it in GitHub Desktop.
Save GalindoSVQ/faeeeb2ab46b5860604a5173e08fa74e to your computer and use it in GitHub Desktop.
import * as React from 'react';
React.useEffectEvent = React.experimental_useEffectEvent;
export default function useInterval(cb: () => void, ms: number) {
const intervalId = React.useRef(null);
const intervalCb = React.useEffectEvent(cb);
const handlerReturn = React.useCallback(() => {
clearInterval(intervalId.current);
}, []);
React.useEffect(() => {
intervalId.current = window.setInterval(intervalCb, ms);
return handlerReturn;
}, [ms, handlerReturn]);
return handlerReturn;
}
@GalindoSVQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment