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

useQuery causes warning in strict mode #7404

Closed
vdineva opened this issue Dec 2, 2020 · 17 comments
Closed

useQuery causes warning in strict mode #7404

vdineva opened this issue Dec 2, 2020 · 17 comments
Assignees

Comments

@vdineva
Copy link

vdineva commented Dec 2, 2020

When I wrap my app in <React.StrictMode />, some of my components cause the following warning:

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 a useEffect cleanup function.

These warning disappear outside of Strict Mode and in production builds (which turn off the StrictMode behavior). The components in question don’t set state or use useEffect. They all use useQuery however.

The warnings disappear when I remove useQuery, i.e. by replacing this:

const { data: { preference = {} } = {} } = useQuery(PREFERENCE)
with an example response, like this:

const { data: { preference = {} } = {} } = {
  data: {
    preference: {
      id: 'dc1d27d7-c5fe-4de6-97c5-9db2bfaa7cd8',
      toc: false
    }
  }
}

and leaving the rest of the component unchanged.

I am using "@apollo/client": "^3.3.2"
This issue was originally reposted in react-apollo repo: apollographql/react-apollo#3635

@jjalonso
Copy link

+1 same here.

@graniczny
Copy link

+1 and here

@BuntyBru
Copy link

same here

@jcreighton jcreighton self-assigned this Jan 8, 2021
@Nightbr
Copy link

Nightbr commented Jan 21, 2021

Hey, I still have this issue with 3.3.7.

I though it was fixed here: #6216

Any leads on this issue or a workaround? Thanks 👍

@stevejay
Copy link

@Nightbr #6216 had to be reverted because the fix broke SSR.

@MelodicsGreg
Copy link

MelodicsGreg commented Jan 28, 2021

From what I can tell by inspecting the cache state, useQuery watches are not being cleaned up when components unmount - it would make sense to me that resolving this would stop the broadcast of cache modifications throwing this error?

In my case, navigating back and forth between the same two pages using react router results in an ever growing list of watches for the same two queries.

This looks like a memory leak, though I haven't checked to see if there is some other mechanism for gc that happens based on some trigger other than components unmounting.

@shreyashc
Copy link

+1

@f4z3k4s
Copy link

f4z3k4s commented Feb 5, 2021

+1, the same

@jcreighton
Copy link
Contributor

The warnings should be silenced with the recent change in #7655. I do want to note though that this doesn't solve the actual issue: useQuery creates duplicate queries in React.StrictMode. A solution for this problem will take longer but in the meanwhile, you should see less warnings in the console.

@macrael
Copy link

macrael commented Jun 27, 2021

@jcreighton is there an issue tracking fixing useQuery to be torn down correctly under StrictMode? I hit some unexpected behavior with refetchQueries that was different in dev and prod.

@dchenk
Copy link

dchenk commented Jul 11, 2021

I'm still seeing this warning sometimes in strict mode at version 3.3.20

@brainkim
Copy link
Contributor

@dchenk Can you try the beta release (npm i @apollo/client@beta)? It might be fixed on there.

@bstro
Copy link

bstro commented Jul 21, 2021

The beta release cleared those warnings for me.

@DennieMello
Copy link

Installing the beta really helped. When to wait for the release?

@brainkim
Copy link
Contributor

@DennieMello This week!

@yogesnsamy
Copy link

@DennieMello This week!

@brainkim I'm on version 3.3.20. Should I be installing @apollo/client@beta to fix this issue?

@brainkim
Copy link
Contributor

brainkim commented Aug 6, 2021

@yogesnsamy Try 3.4.5!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests