Files
probo/packages/hooks/src/useCleanup.ts
Émile Ré 57caa9d28f Move route utils to packages/routes
Signed-off-by: Émile Ré <nemile.re@gmail.com>
2025-12-04 17:27:48 +04:00

20 lines
481 B
TypeScript

import { useEffect, useRef } from "react";
/**
* Hook to handle cleanup after a delay
*
* Used for disposing the graphQL query when the component unmounts
*/
export function useCleanup(callback: () => void, delay: number) {
const timer = useRef<ReturnType<typeof setTimeout>>(null);
useEffect(() => {
if (timer.current) {
clearTimeout(timer.current);
}
return () => {
timer.current = setTimeout(callback, delay);
};
}, [callback, delay]);
}