Created
November 4, 2023 17:34
-
-
Save GalindoSVQ/faeeeb2ab46b5860604a5173e08fa74e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stackblitz link: https://stackblitz.com/edit/vitejs-vite-fet3nx?file=src%2FuseInterval.ts