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

React 16.13.0 'Warning: Cannot update a component from inside the function body of a different component.' #600

Closed
hugo opened this issue Mar 10, 2020 · 6 comments · Fixed by #630

Comments

@hugo
Copy link

hugo commented Mar 10, 2020

After upgrading to React 16.3, I notice a significant performance issue on pages where I have multiple components all querying the same cache value. This appears to be due to an issue with the useQuery hook and/or the cacheExchange.

Expected behaviour
A single network requests is made to populate the cache. Components render with cached value once available.

Actual behaviour
Performance absolutely tanks and the following message appears: Warning: Cannot update a component from inside the function body of a different component.

Screenshot 2020-03-10 at 19 12 41


This doesn't seem to be unique to urqlApollo has a similar issue. I've forked and tweaked the Apollo example repo to use urql instead of apollo. The same notes about when the issue manifests seem to apply.

@JoviDeCroock
Copy link
Collaborator

Hey,

Thanks for the accurate reproduction, this issue seems to be present in most state-management solutions judging from this facebook/react#18178 (comment) I'll follow this up more in the coming days.

@kitten
Copy link
Member

kitten commented Mar 10, 2020

If I'm not mistaken, this can very easily be triggered. As far as urql is concerned, results and operations are global. They're then filtered back down. So when you're writing useQuery that triggers an operation, which then comes back with a result. When you duplicate this query then a result from one query will also update the other one. The only way for us to prevent this would be to deduplicate changes when the update is deeply equal to the last state, which seems counter productive 😢

@hugo
Copy link
Author

hugo commented Mar 10, 2020

Yeah, I find the warning very counterintuitive in this particular situation. If I have two components calling useQuery with the exact same query and variables, I want them to be "linked" together in this way. It's especially baffling to me, a mere mortal, that if I set all my queries to network-only then everything is fine but if I try to use the cache I get literally thousands of these warnings printed in the console, in my particular app.

@JoviDeCroock
Copy link
Collaborator

The error message itself feels kinda erroneous as well to me, let's state the following example

const MyUsers = () => {
  const [result] = useQuery(...)
  ....
}

This errors out if the result is served synchronously from cache but the error states:

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

We aren't leaving the MyUsers component at all, it had me very confused in the start.

@hugo
Copy link
Author

hugo commented Mar 17, 2020

Thanks for the quick fix here, folks 🙏

@shehrayar
Copy link

I am facing this warning: cannot update a component ( forwardref(basenavigationcontainer) ) while rendering a different component can anybody please fix this? I will share my code. looking forward to a positive response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants