Created
September 11, 2021 01:25
-
-
Save MeetMartin/1650ad36014f1a4912cdd64e48aa6427 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 React from 'react'; | |
import useInterval from '../hooks/useInterval'; | |
const MyIntervalComponent = () => { | |
useInterval(() => console.log('Hello World'))(5000); | |
return (<p>Output 'Hello World' to console every five seconds.</p>); | |
}; | |
export default MyIntervalComponent; |
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 { useEffect } from 'react'; | |
const useInterval = fn => time => { | |
return useEffect(() => { | |
const myInterval = setInterval(fn, time); | |
return () => clearInterval(myInterval); | |
}, []); | |
}; | |
export default useInterval; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment