react-apollo
react-apollo copied to clipboard
Fixed unmounted component state update
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: 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/
@benjamn @hwillson Should I move this PR to the apollo-client repository? This patch will need to be applied on both.