Skip to content

Instantly share code, notes, and snippets.

@dhruvilp
Created January 24, 2022 16:17
Show Gist options
  • Save dhruvilp/1a9c28c07eb1dba80ea524dd98854647 to your computer and use it in GitHub Desktop.
Save dhruvilp/1a9c28c07eb1dba80ea524dd98854647 to your computer and use it in GitHub Desktop.
React Hook Interval
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