App: add helper hooks
This commit is contained in:
12
src/hooks/useUnmount.ts
Normal file
12
src/hooks/useUnmount.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { useRef, useEffect } from "react";
|
||||
|
||||
const useUnmount = (fn: () => any): void => {
|
||||
const fnRef = useRef(fn);
|
||||
|
||||
// update the ref each render so if it change the newest callback will be invoked
|
||||
fnRef.current = fn;
|
||||
|
||||
useEffect(() => () => fnRef.current(), []);
|
||||
};
|
||||
|
||||
export default useUnmount;
|
||||
Reference in New Issue
Block a user