Created
January 24, 2022 16:17
-
-
Save dhruvilp/1a9c28c07eb1dba80ea524dd98854647 to your computer and use it in GitHub Desktop.
React Hook Interval
This file contains 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'; | |
export default function useIntervalHook(minutes = 5) { | |
const interval = minutes * 60 * 1000; | |
function refresh(){ | |
fetch('/api/**', {mode: 'no-cors', credentials: 'include'}) | |
.catch(error => { | |
console.log(`There was a problem... ${error.message}`); | |
}); | |
} | |
useEffect(() => { | |
const refreshInterval = setInterval(refresh, interval); | |
return () => clearInterval(refreshInterval); | |
}, [interval]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment