Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Fixed unmounted component state update #4021

Open
wants to merge 1 commit into
base: release-3.x
Choose a base branch
from

Conversation

mccraveiro
Copy link

In my project I'm getting the following error with useQuery:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s, a useEffect cleanup function,

After some debugging, I came into this line:

Promise.resolve().then(forceUpdate);

Because of the re-render in a microtask this may cause the hook to update the state of an unmounted component. Adding a isMounted check fixed the issue.

I'm not sure how to add tests for this. Any suggestions?

@apollo-cla
Copy link

@mccraveiro: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@mccraveiro
Copy link
Author

@benjamn @hwillson Should I move this PR to the apollo-client repository? This patch will need to be applied on both.

Copy link

@renanmav renanmav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and I can confirm that these changes work

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants