react: Bug: too hard to fix "Cannot update a component from inside the function body of a different component."
Note: React 16.13.1 fixed some cases where this was overfiring. If upgrading React and ReactDOM to 16.13.1 doesn’t fix the warning, read this: https://github.com/facebook/react/issues/18178#issuecomment-595846312
React version:
16.13.0
Steps To Reproduce
- Build a time machine.
- Go to the year 2017.
- Build a huge application of 10K lines of code.
- Get 80 (!) dependencies at package.json file including ones that become no longer maintained.
- Update React to the latest version at February 27, 2020.
- Get tons of errors that you don’t know how to fix.
- Tell your client that fixes are going to take unknown time and it’s going to cost $$$ + days or weeks of investigation or we’re going to get stuck with the outdated version of React and related libraries forever which will cost more $$$ but later.
Being serious, the business I work for isn’t interested on that at all. Obviously I’d never made it happen to get such warnings to appear if I’d get them earlier. Currently that’s impossibly hard to make the errors to be fixed because I get them at many different cases and with a huge stack trace. I tried to fix at least one of the appearing errors and it already took a lot of time. I tried to debug some of used libraries but got no luck.
Just one example:

There we can notice the use of an outdated react-router, an outdated redux-connect (which I had to put to the project source to fix errors of outdated componentWillReceiveProps method), some HOCs created by recompose etc. It isn’t just a simple virtual DOM tree where I can walk thru components developed by me and search by setState string to fix the bug, that’s way more complicated than that.
Please make an “UNSAFE” option to disable this error or provide a simpler way to find where the error is thrown 🙏
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 29
- Comments: 109 (16 by maintainers)
To the future commenters. I understand seeing a new warning is frustrating. But it points out legitimate issues that are likely causing bugs in your code. We would very much appreciate if you could refrain from expressing sarcasm and annoyance.
If you can’t understand where it’s coming from in the stack traces, please post screenshots or create reproducing sandboxes and we’ll try to help. Most of these are probably coming from a few libraries, so the most productive thing to do is to reduce these cases and then file issues with those libraries.
Thank you all.
We fixed the cases where the warning was over-firing in 16.13.1. The remaining cases are legit and need to be fixed.
I wish we had added this warning earlier. I’m sorry we didn’t. It was an oversight with the introduction of Hooks. I believe this must be caused by newer code that uses Hooks, because there was already the same warning for classes much earlier so any earlier code would’ve already seen this warning.
Note that this will likely start hard failing in future versions of React. Intentional or otherwise (we’ve had a lot of bugs with this pattern). So regardless, you might end up getting stuck on an old version. If it’s not possible to fix it, I’d recommend pinning to an older version of React.
However, that said, we want to help you and make it as easy as possible to fix these, including seeing if we can get help from library authors to publish fixed versions.
If you expand the little
>arrow in the Chrome console you should also see an additional Stack trace (in addition to the Component stack in your screen shot). Can you post that too? That should show you the exact callsite that is causing a side-effect in render.I think it is imperative that this error message be modified to include exactly which line of code is causing the error. It’s almost impossible to pinpoint whether it is local code or library code that’s causing the issue. External librairies
libraries like react-redux and react-router are most likely the culpritmay very well be the culprits yet it is impossible to easily determine that.@sebmarkbage thank you for the response. The stacktrace appearing after clicking > is ridiculous. It contains 200+ items!
I was going to paste it there or give a link to pastebin but tried a different direction. I walked thru Github issues of some of used libraries and found out that one of suspects is redux-form: https://github.com/redux-form/redux-form/issues/4619. I hope that’s the only library which causes the errors and I’m going to wait for a fix before upgrading React.
But still, I’d ask to not close the this issue and I propose other developers to mention here libraries which also cause these errors.
@l0gicgate
There are limits to what we can do in JavaScript. But all the information is in the stack you see in the browser. All you need to do is to skip the lines that are inside React.
To see the JavaScript stack, you need to click on a small arrow next to the error message.
For example, look at this screenshot from earlier:
I appreciate it’s a bit annoying to dig through the stack, but it shouldn’t be that hard to skip the first several frames. The very next frame is the source of the problem. In this case, it seems like something inside the Formik library. So you can file an issue with it.
Small update on this thread
We’re going to release a patch of React soon that fixes over-firing of this warning for classes. So if you experience this, consider waiting a few days and then trying 16.13.1 when it’s out.
If you want to keep looking for the causes, I hope https://github.com/facebook/react/issues/18178#issuecomment-595846312 explains how.
@LabhanshAgrawal That’s correct. Some background explanation here:
Your example is actually one of the early mistakes people make with react. I’m not sure it’s exactly the same issue that’s being discussed here. Your line here:
onClick={setMobileNavOpen(false)is calling the function during the button tender, not on click. That’s why wrapping it in a arrow function fixes it.This is starting to crop up more and more in third party libraries now:
urql, Apollo.I ran into this and for several hours I assumed the problem was in my code. The condensed stacktrace points at my components, and it’s not unusual for me to see third party libraries in the expanded stacktrace when I did explicitly trigger an error. From my (albeit limited) research into this particular warning, it seems that most developers are not causing this issue themselves, but rather depending on code that does. Usually it’s good practice to assume it isn’t a bug upstream but when it is an upstream bug, wasting time tracking down a problem in your code that doesn’t exist is rather frustrating. Is there anything React can do to help an average user determine if it was code they wrote, or code they depend upon that caused the issue?
One thing I note from the Apollo issue is:
If this is correct, can React give more information here? Perhaps telling us both the initiating component and the components that it caused to be updated?
I ran into this recently and the fix was wrapping
setStatehandler call sites inuseEffect, like so: https://github.com/airbnb/lunar/commit/db08613d46ea21089ead3e7b5cfff995f15c69a7#diff-1c3bdd397b1ce5064142488877045306R56 (onChangeandonSubmitusesetStatehigher up the chain).@martinezwilmer Where is
onPriceUpdatedbeing called? Maybe try wrapping it inuseEffect?@LabhanshAgrawal In your stack,
UNSAFE_componentWillReceivePropscalls somefitResizewhich dispatches a Redux action, which in turn updates a bunch of components. Hence the problem. So yeah, changing that tocomponentDidUpdateworks.@suhanw In your stack, something called
ModuleImpressionTrackerappears to dispatch a Redux action during a constructor. Constructors shouldn’t have side effects. I think that’s the cause of the issue, not your Hook.As I said upthread, I would be very surprised if whatever’s happening here is an issue with React-Redux.
That said, I’m also not even sure exactly what triggers this message in the first place. I half-get what the error message is saying, but what kind of app code pattern would actually be an example of that behavior?
@gaearon just upgraded and the error disappeared! Thank you guys for your work!
Calling setState of your own component during render is a supported pattern (although it should be used sparingly). It’s setState on other components that are bad. You can’t detect those statically.
@MeLlamoPablo
Not strongly. I think it mostly happens to work in the current version of React, but that may well change in the future. Even today, combined with features like
lazyandSuspenseit is not guaranteed.Sibling order is not guaranteed. For parent/child order, you’re right parents have to render first; however, React may need to re-render the parent again before it gets to a child, or even after it has rendered the first child but before the second one. Again, once you add features like code splitting, you lose guarantees even faster.
This is fragile.
Yeah, this is one of the cases I referred to in https://github.com/facebook/react/issues/18178#issuecomment-600369392. We’ll mute the warning in this case. The warning itself is legit and as you rightly say, conceptually it’s a problem in classes too. However, the discrepancy doesn’t make sense so we’ll mute it for both cases for now when it’s coming from a class (which it is, in this example).
@gaearon I wanted to create a codesandbox but the latest version of the library has some issues with it. Error is not thrown in the old versions. At the moment it seems that the only way to reproduce it is to spin it up in Create React App locally 😦
I guess even if it’s more than intended, in my opinion it’s a good thing to have as it helps in improving our projects using classes.
@Glinkis Yeah maybe let’s wrap it up here but my suggestion would be to read
useRouteMatchin the parent component, pass the ID as a prop, and then read props in selector like you normally would. (I don’t actually know how this is done in Redux these days but there must be some way.)OK so looking deeper into this, I think we’re warning in more cases (e.g. for classes) than we originally intended. We’ll look into silencing some of those.
We were facing this issue in Hyper but we are not using hooks, and couldn’t find anything in the render call from the call stack. But there was a call in
UNSAFE_componentWillReceivePropsin the stack. Updating that withcomponentDidUpdatefixed the issue for us https://github.com/zeit/hyper/pull/4382 Posted it here if it might help someone@samcooke98 I’ve opened a PR for this https://github.com/facebook/react/pull/18316
I’m new to react but I had code like this:
Which resulted in:
Cannot update a component from inside the function body of a different component.All I had to do is add an arrow function to setMobileNavOpen in MobileNav like so:
And that fixes the problem, hope this helps someone!
@sebmarkbage I can no longer reproduce this problem. We have update some libraries and the problems over.
Hard to find the precise line concerned by the warning here:
@gaearon do you again have one tip about it ?
I have the same problem and I’m seeing that the warning is showing the first time that i write in my redux field or when i clear all it
I face the same problem with Redux-form!