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

Can't perform a React state update on an unmounted component. #627

Open
tfnribeiro opened this issue Nov 11, 2024 · 1 comment
Open

Can't perform a React state update on an unmounted component. #627

tfnribeiro opened this issue Nov 11, 2024 · 1 comment

Comments

@tfnribeiro
Copy link
Contributor

We have a few of these types of errors across the application:

image

This happens whenever we unmount the component and an async call wants to update a state that no longer exists. This is easy to fix, if we we could clear the functions when we know the function is being unmounted, but when we have more complex calls, it's hard to know which function exactly is trying to update.

I saw a possible solution in this discussion: https://stackoverflow.com/questions/53949393/cant-perform-a-react-state-update-on-an-unmounted-component

Essentially, we can have a reference that is updated to false whenever the component is unmounted, and then all the setters have this condition, is component mounted. We could generalize this logic to be under a hook, so whenever the set is called, we check if the component is mounted.

Not sure if this is important, but I decided to log it in case we see these warnings we know the cause for them.

@mircealungu
Copy link
Member

Thanks for opening this @tfnribeiro. It's not cool to keep warnings in the project. We should fix them.

However, the first solution in that thread I'm not convinced of. If we have a lingering function that does not get cleaned up after a component unmounts, then we should figure out a way of getting rid of the function, not adding one more conditional inside it. In fact, later in the thread we see an answer that argues that a check for component is mounted might actually be an anti-pattern in React. Their solution for fetch with an AbortController might be relevant though... and hopefully we can put it somewhere in our _get or _post methods, so we don't have to call it from every component: that could be unpleasant.

What I don't understand is what do those people mean who say that they upgraded to newer react and they don't have the warning anymore? Are we not using the latest version of React?

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

2 participants