Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roeycohen opened this issue Jul 13, 2020 · 0 comments
Open

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

roeycohen opened this issue Jul 13, 2020 · 0 comments

Comments

@roeycohen
Copy link

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant