Created
June 6, 2019 15:16
-
-
Save qborreda/ecddece84d8f83842e2d96c528ccc80b to your computer and use it in GitHub Desktop.
Revisions
-
qborreda created this gist
Jun 6, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ /** * React custom hook to fire a function given an interval * @usage: useInterval(()=>{...},1000); **/ function useInterval(callback, delay) { const savedCallback = useRef(); useEffect(() => { savedCallback.current = callback; }); useEffect( () => { function tick() { savedCallback.current(); } let id = setInterval(tick, delay); return () => clearInterval(id); }, [delay] ); }