react-is-mounted-hook
react-is-mounted-hook copied to clipboard
why ref is set to true only after the useEffect is called?
Hi,
I believe the whole idea of this code is to prevent changing the state of a component after the component was already removed/unmounted. but what happens if the async call returns somehow before useEffect is called? in that case, we won't keep the original behavior we wanted.
so, doesn't this code makes more sense? (set to ref to true upon initiation):
const ref = useRef(true);
useEffect(() => {
return () => {
ref.current = false;
};
}, []);
return () => ref.current;