react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

Fixed unmounted component state update

Open mccraveiro opened this issue 4 years ago • 2 comments

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: https://github.com/apollographql/react-apollo/blob/4bd40a94b18102b1eb42864d2e59d52ced0303f5/packages/hooks/src/utils/useBaseQuery.ts#L34

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?

mccraveiro avatar Jun 12 '20 12:06 mccraveiro

@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/

apollo-cla avatar Jun 12 '20 12:06 apollo-cla

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

mccraveiro avatar Jun 16 '20 11:06 mccraveiro