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

State update on unmounted component after Apollo cache update

Open tkvlnk opened this issue 4 years ago • 5 comments

Intended behavior: useQuery checks if component is unmounted at the moment when the setState is fired and skips state update on cache update

Actual behavior: When "parent" component and "child" components both listen for same query. And at some point due to the cache update for that listened query "child" should unmount the warning about update of state for unmounted component is fired.

Steps to reporoduce: Codesanbox link

tkvlnk avatar Apr 19 '20 13:04 tkvlnk

I experience this too. I have been trying to figure out how to get rid of the message and so far, the only thing that worked was to set fetchPolicy: "no-cache", but that slows everything down.

I though that maybe #3139 fixed it, but it only seems to affect subscriptions.

msheakoski avatar Jun 02 '20 13:06 msheakoski

Check this comment from the #3635

I managed to sort this out by replacing the useQuery hook with the useLazyQuery and calling it from the useEffect. Hope that will work for you as well.

jvrbnjak89 avatar Jun 02 '20 13:06 jvrbnjak89

Check this comment from the #3635

I managed to sort this out by replacing the useQuery hook with the useLazyQuery and calling it from the useEffect. Hope that will work for you as well.

yeah I know about this workaround but it would be better to have a clean way without redundant tricks

tkvlnk avatar Jun 02 '20 14:06 tkvlnk

Then I think you'll have to wait for this apollo-client fix. apollographql/apollo-client/pull/6216

jvrbnjak89 avatar Jun 02 '20 16:06 jvrbnjak89

Thank you, @jvrbnjak89. It looks like a fix is close to being released, so I will wait for that, but I appreciate the further insight into the issue that you provided!

msheakoski avatar Jun 02 '20 17:06 msheakoski