mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 lines
404 B
TypeScript
14 lines
404 B
TypeScript
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());
|
|
};
|