question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApolloClient 3: Cannot update a component from inside the function body of a different component.

See original GitHub issue

related to https://github.com/apollographql/react-apollo/issues/3863

Intended outcome: useQuery should not throw unintentional state changes warning.

Actual outcome:

Error thrown: Warning: Cannot update a component from inside the function body of a different component.

How to reproduce the issue: probably the same as https://github.com/apollographql/react-apollo/issues/3863#issuecomment-608573622

Versions "@apollo/client": "^3.0.0-beta.44"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
asaadaweycommented, Jun 4, 2021

@Banou26 i think yes

wrapping the apollo code inside a useEffect or setTimeout is not an efficient solution

2reactions
coler-jcommented, Nov 20, 2020

I am getting this when using reactiveVars for pagination in a child component.

I am on @apollo/client@3.2.5

image

I have a reactiveVar:

// file: reactiveVars.ts
export const currentPageVar = makeVar<number>(1)
export const currentDataVar = makeVar<IDataInterface>()

A useQuery call in my root / parent component:

export default function App() {
  const { data } = useQuery(. .......... )
  currentDataVar(data)

  return (<>
     <SomeComponentToRenderData />  
     <SomeChildComponent />
   </>
  )
}

And in a child component when a button is pressed the reactive component is updated, which updates my query via a local client only field:

export default function SomeChildComponent() => {
    const currentPage = useReactiveVar(currentPageVar)

    return <Button onClick={() => currentPageVar(currentPage + 1)}
}

Then my data is updated via the other reactive var

export default function SomeComponentToRenderData() => {
    const currenData = useReactiveVar(currentDataVar)

    return <ul> ....  currentData  .....  </li>
}

This pattern for pagination (i.e. using reactive hooks + client schema fields) appears to cause this error to occur.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix "Cannot update component from inside function body of ...
The login screen calls setUser from the UserContext object on a successful login. This has been working fine but today I updated to...
Read more >
warning: cannot update a component - You.com | The Search ...
To fix cannot update a component while rendering a different component warning with React, we should call functions from props inside the useEffect...
Read more >
How I fixed 'Cannot update a component from inside the ...
In this short article I'll highlight why Cannot update a component from inside the function body of a different component warning happened ...
Read more >
Queries - Apollo GraphQL Docs
As our query executes and the values of loading , error , and data change, the Dogs component can intelligently render different UI...
Read more >
How to fix the "cannot update a component while rendering a ...
← flaviocopes.com. How to fix the "cannot update a component while rendering a different component" error in React. Published in 2020.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found