优化
This commit is contained in:
13
client/shared/hooks/useUnmount.ts
Normal file
13
client/shared/hooks/useUnmount.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useRef } from 'react';
|
||||
import { useEffectOnce } from './useEffectOnce';
|
||||
|
||||
// Reference: https://github.com/streamich/react-use/blob/master/src/useUnmount.ts
|
||||
|
||||
export 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;
|
||||
|
||||
useEffectOnce(() => () => fnRef.current());
|
||||
};
|
||||
Reference in New Issue
Block a user