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

Warning: Can't perform a React state update on an unmounted component. #6209

Closed
amiyatulu opened this issue Apr 28, 2020 · 50 comments
Closed

Comments

@amiyatulu
Copy link

This is the code for useMuation

  const [tokenAuthCall, { loading: mutationLoading, error: mutationError }] = useMutation(LOGIN_MUTATION, {
    refetchQueries: [ { query: ME_QUERY }], awaitRefetchQueries: true,
  })

But on using refetchQueries it gives 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.

This is my nav code:

function NavContents(props) {
  let me
  let data = MeQuery()
  try {
    me = data.me
  } catch {
    me = false
  }
return (
    <React.Fragment>
{me && (
              <li className="nav-item">
                <NavLink to="/profile" className="nav-link">
                  {me.name}
                </NavLink>
              </li>
            )}
            {me && (
              <li className="nav-item">
                <NavLink to="/signout" className="nav-link">
                  (SignOut)
                </NavLink>
              </li>
            )}
<React.Fragment>
)

And the is MeQuery()

import React, { Component } from "react"
import { gql, useQuery } from "@apollo/client"

const ME_QUERY = gql`
  query {
    me {
      email
      name
    }
  }
`

function MeQuery() {
  const { loading, error, data } = useQuery(ME_QUERY)
  let value
  if (loading) return "Loading..."
  if (error) {
    value = false
  }
  value = data
  return value
}

export default MeQuery
export {ME_QUERY}
@amiyatulu amiyatulu changed the title Warning: Can't perform a React state update on an unmounted component. for refetchqueries Warning: Can't perform a React state update on an unmounted component. Apr 28, 2020
@amiyatulu
Copy link
Author

I found the cause. The issue is because of <React.StrictMode> apollographql/react-apollo#3635

@hwillson
Copy link
Member

@amiyatulu is this still happening when you use the latest @apollo/client@beta (3.0.0-beta.50)?

@hwillson hwillson added the 🏓 awaiting-contributor-response requires input from a contributor label May 21, 2020
@amiyatulu
Copy link
Author

Yes, it's still given the error after upgrade to 3.0.0-beta.50

@nodabladam
Copy link

Is this the fix and if so can it get merged?
#6216

@Banou26
Copy link
Contributor

Banou26 commented Jun 8, 2020

Still happening for me with useQuery on 3.0.0-rc.2

Edit: Oh well, just saw #6395 (comment) that talks about #6216's revert

@Banou26
Copy link
Contributor

Banou26 commented Jun 13, 2020

Error seems to be gone for me on 3.0.0-rc.3

@devinowen
Copy link

I'm still seeing this error when using useQuery with a basic query and the follow params:

{
  fetchPolicy: "no-cache",
  errorPolicy: "all"
}

My useEffect dependency array includes the data, loading, and error vars returned from useQuery, if that makes a difference.

I'm using StrictMode and on "@apollo/client": "3.0.0-rc.5".

Happy to provide more details if needed!

mikemoraned added a commit to mikemoraned/playout that referenced this issue Jun 28, 2020
- note that this currently causes the following
  warning whenever a game is completed, as it
  causes the `GetRecentlyCompleted` query to be
  revaluated even though the component isn't
  currently visible, and mounted
  ```
  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.
    in RecentlyCompleted (at Start.js:145)
    in Start (at App.js:48)
  ```
  this is a known issue in Apollo, which as of
  writing is still being worked on:
  apollographql/apollo-client#6209
@ablbol
Copy link

ablbol commented Jul 16, 2020

I have been tracking this warning for a while and I can confirm that useQuery is the source of the warning. The warning says 'Warning: Can't perform a React state update on an unmounted component'. I am using:

"@apollo/client": "^3.0.1",
"aws-appsync": "^4.0.0",
"graphql": "^15.3.0",
"react": "16.13.1",
"react-native": "0.63.1",

It is happening mostly in a page where the component is un-mounting . useQuery is reading from cache. Hope this help fix the problem.
Thanks

@csandanov
Copy link

I wasn't sure if my similar issue (more like #6248) is a bug or not so I posted it with more details on SO https://stackoverflow.com/questions/62938649/react-apollo-client-arning-cant-perform-a-react-state-update-on-an-unmounted

@Felix-Indoing
Copy link

Happened also when route changes while current route has a useQuery

@figurluk
Copy link

@Felix-Indoing same for me 😕

@daltonmenezes
Copy link

Same here

@nodabladam
Copy link

Every time I get "state update on an unmounted component" in the console, this issue makes it hard to tell if it is really due to state in my code or not and if I have actually fixed legitimate issues because this issue causes so many errors. If there is a way to fix this without breaking SSR, it would really help.

@an-tran-eda
Copy link

same here

@trieu-le-eda
Copy link

I got the same issue when the route changes with "@apollo/react-hooks": "^3.1.5"

@jalho
Copy link

jalho commented Aug 19, 2020

I was struggling with a similar issue an solved it by using useLazyQuery on mount instead of useQuery on each render, as inspired by jordicasesnoves:

import { useEffect } from "react";
import { useLazyQuery } from "@apollo/client";

  // ...
  const [myQueryExecutor, { data }] = useLazyQuery(MY_QUERY);

  // execute query on component mount
  useEffect(
    () => { myQueryExecutor(); },
    [myQueryExecutor]
  );

@axelvaindal
Copy link

I got the same issue as well with @apollo/client version 3.1.1 on route change when the current route is using useQuery.

@rediska1114
Copy link

same here

@azinit
Copy link

azinit commented Nov 29, 2020

Still actual!!

@jjalonso
Copy link

still failing.

@manzoorwanijk
Copy link
Contributor

Still the same warnings :(

@kelseyway
Copy link

Same.

@suhanw
Copy link

suhanw commented Mar 26, 2021

That seemed to do the trick, thanks @davidgilbertson!

@eden-lane
Copy link

@suhanw do you have your <RouterProvider> outside your <ApolloProvider> in your component hierarchy? Does reversing them make a difference?

unfortunately it changed nothing for me, still getting this error

@ndeamador
Copy link

ndeamador commented Apr 7, 2021

I also tried wrapping ApolloProvider inside BrowserRouter in index.ts (before I had BrowserRouter wrapping my routes in App.js) and I am still getting the error:

` ReactDOM.render(

  <ApolloProvider client={client}>

    <React.StrictMode>

      <App />

    </React.StrictMode>

  </ApolloProvider>

</Router>,

document.getElementById('root')

);
`

I have also tried swapping Router and ApolloProvider in the code above and I still get the warning.

I can confirm though that using useLazyQuery + useEffect gets rid of the warning. Not sure if this is intended or ideal.

ndeamador added a commit to ndeamador/game-affinity-project-client that referenced this issue Apr 7, 2021
@PierreAndreis
Copy link

It might be related to #8011

@mvaivre
Copy link

mvaivre commented Apr 28, 2021

Is there any update regarding this? Still the same warning when using useQuery and changing route. And it seems to me that using useLazyQuery and useEffect is a workaround, not an actual solution?

@jgwiazdowski
Copy link

it's still an issue

so only solution now is lazy query and useEffect?

@Jojocaster
Copy link

@jgwiazdowski The best workaround so far (in my opinion) is to use a hook telling you whether a component is mounted or not and to pass that Boolean to the skip parameter of your query.
It is a bit cleaner than useLazyQuery.

@maxdbarton97
Copy link

useLazyQuery and useEffect does not work for me sadly.

Any movement on this?

@ibrahimhist
Copy link

i have encountered same problem. I tried every possibility. This is the solution that help me with. Using skip { skip:true } after getting data solves

const [skip, setSkip] = useState(false)
const [securities, isLoading] = useQueryGetMinifiedSecurities(skip)
const [datasource, setDatasource] = useState([])

 useEffect(() => {
    if (securities && !isLoading) {
      setDatasource(securities)
      setSkip(true)
    }
  }, [isLoading, securities])

@PierreAndreis
Copy link

This issue is related to StrictMode, and I have a working reproduction on #8011

@brainkim brainkim reopened this Jul 7, 2021
@brainkim brainkim modified the milestones: June 2021, July 2021 Jul 7, 2021
@mishasyrbu
Copy link

useEffect(() => {
    if (data) {
      // here call navigate action
    }
  }, [onClose, data]);

@hwillson
Copy link
Member

hwillson commented Aug 3, 2021

This should be fixed with @apollo/client >= 3.4 - thanks!

@hwillson hwillson closed this as completed Aug 3, 2021
@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