Warning: Cannot update a component from inside the function body of a different component.
Are you submitting a bug report or a feature request?
bug report
What is the current behavior?
After npm upgrading to the latest React, I am getting the following warning in my console from the React Developer tools Chrome extension: "Warning: Cannot update a component from inside the function body of a different component."
The component trace just points to a line with a boring/standard use of Field in JSX. Quickly going through the app I'm working on, this warning happens everywhere, on every use of Field in the app. For example, the warnings point to lines like the 2nd to last line (RadioField = ...) of this file (please excuse the coding style here). Again, there is nothing special about this code; it's happening on every Form that has components that use Field.
import React from "react";
import { Field } from "react-final-form";
const Radio = ({label,input,children,className}) => {
const { checked } = input;
return (
<label className={"input-radio "+ (checked? "is-checked ": "")+
className}>
{children && children}
<input
{...input}
className="form-radio"
/>
<var title={label}>{label}</var>
</label>
);
};
Radio.defaultProps = { label:"", meta:{}, input:{}, children:[] };
const RadioField = props => <Field {...props} component={Radio} type="radio"/>;
export default RadioField;
What is the expected behavior?
No warning
Sandbox Link
I'll make one if you need it, but I think the link below in Other Information succinctly explains what is happening and a hooks based workaround that react-final-form can implement.
What's your environment?
ChromeOS 79 React 16.13.0 released Feb 26th, 2020 Final Form 4.18.7 React Final Form 6.3.5
Other information
See here: https://reactjs.org/blog/2020/02/26/react-v16.13.0.html#warnings-for-some-updates-during-render
I'm also seeing this warning after updating to React 16.13.0. For me, it only seems to occur when a field is conditionally rendered.
Sandbox link: https://codesandbox.io/s/empty-forest-6m0ri?fontsize=14&hidenavigation=1&theme=dark
A good example of this happening is the Wizard Form Example. Any time the Previous or Next buttons are pressed, just about every Field from all pages throws this warning.
I'm also seeing this warning after updating to React 16.13.0. For me, it only seems to occur when a field is conditionally rendered.
Sandbox link: https://codesandbox.io/s/empty-forest-6m0ri?fontsize=14&hidenavigation=1&theme=dark
Yes, I got this error for a select box that is dynamically rendered.
Same error here. Using final-form-array produces this error as well.
Not a fix. But i realized that when i added the subscription prop to the form:
( subscription={{ submitting: true, pristine: true }} in my case)
The errors disappeared.
A lot of popular libraries are affected by this warning : https://github.com/facebook/react/issues/18178 It's a warning party in my console right now as I also use urql which is affected as well. 😂
I did some analysis on this issue and found the source of the problem, at least for conditionally rendered fields, but this might also be the cause in other cases:
https://github.com/final-form/react-final-form/blob/f3c1aea93b520c0620f1b8090f5966e0da94a798/src/useField.js#L90-L105
In here, the field is synchronously registered, which causes a synchronous update to the form state, which in turn synchronously triggers setState() on the Form:
https://github.com/final-form/react-final-form/blob/f3c1aea93b520c0620f1b8090f5966e0da94a798/src/ReactFinalForm.js#L97
So what it boils down to is that the mounting of a Field directly calls setState() on the Form. This is apparently not a problem in some cases, but in others. I'll have to do some additional analysis to figure how how to rewrite that code to use useEffect() instead. If anyone else already has a good idea, just reply, I'll keep a close eye on this thread.
So, after looking (and trying to understand what the heck all the code is doing), my personal conclusion is this:
- Instead of
useField()registering the field inuseState()initially, it should only be registered in theuseEffect()bellow it (that is already done). - While the field is not registered,
useField()would yieldnulluntil registration occurred. Fieldwould have to check for the returnedFieldRenderPropsto be null, and not render until it received a non-null value.
Upside of this idea: very easy to implement (just a couple of lines have to be changed), almost no side-effects.
Downside: it changes the useField() API, which is a public one, so this would affect projects using it directly instead of the Field component.
Personally, I see no other way to fix this problem, I'd love to hear the thoughts of one of the maintainers about this issue. If they'd agree with my conclusion, I'd go ahead and write a pull request, or they can implement that fix.
@erikras, any take on below approach?
So, after looking (and trying to understand what the heck all the code is doing), my personal conclusion is this:
- Instead of
useField()registering the field inuseState()initially, it should only be registered in theuseEffect()bellow it (that is already done).- While the field is not registered,
useField()would yieldnulluntil registration occurred.Fieldwould have to check for the returnedFieldRenderPropsto be null, and not render until it received a non-null value.Upside of this idea: very easy to implement (just a couple of lines have to be changed), almost no side-effects. Downside: it changes the
useField()API, which is a public one, so this would affect projects using it directly instead of theFieldcomponent.Personally, I see no other way to fix this problem, I'd love to hear the thoughts of one of the maintainers about this issue. If they'd agree with my conclusion, I'd go ahead and write a pull request, or they can implement that fix.
@yann-combarnous, @erikras Any update on this?
@yann-combarnous, @erikras Any update on this?
I am not a maintainer, just an interested party like you :).
I spent a whole day looking for a solution for this issue and so far I manage to silent this error by pausing the form validations with form.pauseValidation(). In our used case this error only happens when I have a field with a validator function, I don't know why but final form is validating the initial value of the field which in most cases will be a nil value.
Adding something like do the trick:
pauseValidation();
useEffect(() => resumeValidation(), [resumeValidation]);
But, final-form is overriding the isValidationPause flag internally in some cases.
That seems more like a workaround instead of actually solving the problem (like my recommendation would). Still waiting on a word from the maintainer though :)
Hi guys. Sorry to be so late on this. I got laid off at work and have had two children locked in the house with me for weeks.
Upside of this idea: very easy to implement (just a couple of lines have to be changed), almost no side-effects.
A lot have work has been done to ensure that things look right "on first render", which is why things are the way they are. This is going to be a major breaking change. I just tried it and 75% of the unit tests failed.
Downside: it changes the useField() API, which is a public one, so this would affect projects using it directly instead of the Field component.
Yep.
This needs more investigation.
No worries, everything will be fine @erikras . 🍻
Don't understand how an elite developer like you would be laid off, even now... Also, try Upwork. With your resume, you should be making a lot of money immediately working remotely.
On Mon, Mar 30, 2020, 6:46 AM Erik Rasmussen [email protected] wrote:
Hi guys. Sorry to be so late on this. I got laid off at work and have had two children locked in the house with me for weeks.
Upside of this idea: very easy to implement (just a couple of lines have to be changed), almost no side-effects.
A lot have work has been done to ensure that things look right "on first render", which is why things are the way they are. This is going to be a major breaking change. I just tried it and 75% of the unit tests failed.
Downside: it changes the useField() API, which is a public one, so this would affect projects using it directly instead of the Field component.
Yep.
This needs more investigation.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/final-form/react-final-form/issues/751#issuecomment-605951654, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2O764S54EQ7INNMQ5UGLTRKCBBFANCNFSM4K7IAAGQ .
Yeah, I'm gonna get a job quickly; have several offers already, but it still adds additional stress to the already elevated global level.
Companies restructure and my position disappeared out from under me. Wasn't to do with my performance or skill level.
Another idea, but that one's kinda hacky, which is why I didn't propose it: Have the setState() in the Form be called via window.requestAnimationFrame().
I have some good news for you guys....
@erikras Just looking at the code and it is a tad bit hard to follow for me... would you mind explaining why notifyFieldListeners() has an optional 'name' passed into it and the usage there? It is called quite a bit and seems to only get name passed in when silent is true now? Not sure what 'safeFields' is.