committed by
Sacha Al Himdani
parent
44898be9d3
commit
55e85e5f67
19
apps/trust/src/hooks/useDelayedEffect.ts
Normal file
19
apps/trust/src/hooks/useDelayedEffect.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
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]);
|
||||
}
|
||||
Reference in New Issue
Block a user