实现
一、认识
二、实现
2.1 /hooks/useUnmount.tsx
import { useEffect } from 'react';
import useLatest from './useLatest';
function useUnmount(fn) {
const fnRef = useLatest(fn);
useEffect(
() => () => {
fnRef.current();
},
[]
);
}
export default useUnmount;
2.2 /hooks/useLatest.tsx
import { useRef } from 'react';
function useLatest(value) {
const ref = useRef(value);
ref.current = value;
return ref;
}
export default useLatest;