add a useInterval hook and move useNetwork hook to the hook folder

This commit is contained in:
Audric Ackermann 2020-11-18 14:40:59 +11:00
parent 828aa4413f
commit b2e362a36b
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
2 changed files with 25 additions and 0 deletions

25
ts/hooks/useInterval.ts Normal file
View File

@ -0,0 +1,25 @@
import React from 'react';
export const useInterval = (callback: any, delay: number | null) => {
const savedCallback = React.useRef<any>();
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);
React.useEffect(() => {
function tick() {
if (savedCallback && savedCallback.current && savedCallback.current) {
savedCallback.current();
}
}
if (delay !== null) {
const id = global.setInterval(tick, delay);
tick();
return () => {
global.clearInterval(id);
};
}
return;
}, [delay]);
};