Created
May 8, 2020 21:40
-
-
Save andycarrell/d54742f2d089c4e942014af558b47def 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
export default function useTimeout() { | |
const ids = useRef([]); | |
React.useEffect(() => { | |
const cleanupTimeouts = () => { | |
ids.current.forEach(clearTimeout); | |
}; | |
return cleanupTimeouts; | |
}, []); | |
// setTimeout takes a callback and a delay, then returns the id associated with the timeout. | |
return useCallback((callback, delay) => { | |
const id = setTimeout(callback, delay); | |
ids.current.push(id); | |
return id; | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment