29 lines
561 B
JavaScript
29 lines
561 B
JavaScript
import {useRef, useEffect} from 'react';
|
|
|
|
/* React hook for setInterval */
|
|
export function useInterval(callback, delay) {
|
|
const savedCallback = useRef();
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
});
|
|
|
|
useEffect(() => {
|
|
function tick() {
|
|
savedCallback.current();
|
|
}
|
|
|
|
if(delay > -1) {
|
|
let id = setInterval(tick, delay);
|
|
return () => clearInterval(id);
|
|
}
|
|
}, [delay]);
|
|
}
|
|
|
|
export function usePrevious(value) {
|
|
const ref = useRef();
|
|
useEffect(() => {
|
|
ref.current = value;
|
|
});
|
|
return ref.current;
|
|
} |