react-is-mounted-hook icon indicating copy to clipboard operation
react-is-mounted-hook copied to clipboard

why ref is set to true only after the useEffect is called?

Open roeycohen opened this issue 5 years ago • 0 comments

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;

roeycohen avatar Jul 13 '20 13:07 roeycohen