♻️ Refactor hooks

This commit is contained in:
Manuel Ruwe
2022-12-23 17:29:58 +01:00
parent f3b601dc2d
commit c84d8b59fe
15 changed files with 17 additions and 17 deletions

View File

@@ -0,0 +1,22 @@
import { useEffect, useRef } from 'react';
export function useSetSafeInterval() {
const timers = useRef<NodeJS.Timer[]>([]);
function setSafeInterval(callback: () => void, delay: number) {
const newInterval = setInterval(callback, delay);
timers.current.push(newInterval);
return newInterval;
}
useEffect(
() => () => {
timers.current.forEach((t) => {
clearInterval(t);
});
},
[]
);
return setSafeInterval;
}