Skip to content

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

@roeycohen

Description

@roeycohen

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions