实现
createUpdateEffect/index.js
import { useRef } from "react";
export function createUpdateEffect(hook) {
return function (effect, deps) {
const isMounted = useRef(false);
hook(() => {
return () => (isMounted.current = false);
}, []);
hook(() => {
if (!isMounted.current) {
isMounted.current = true;
} else {
return effect();
}
}, deps);
};
}