-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
call props.onChange on Form component mount to set any defaults in sc… #1034
call props.onChange on Form component mount to set any defaults in sc… #1034
Conversation
Thanks for the great write-up! (And sorry for taking so long to get to this.) My intuition is that if we use a Looking at the code, I see that Form calls |
Thanks for taking a look at this. I see what you're saying about the onChange callback only being called when the data has actually changed. I can do a deep compare of the the state vs the props formData in the componentDidMount() {
if (this.props.onChange
&& !deepEquals(this.state.formData, this.props.formData)) {
this.props.onChange(this.state);
}
} Would you be happy with that? To your second point about the other lifecycle events, I think in theory // Form.js
componentWillReceiveProps(nextProps) {
const nextState = this.getStateFromProps(nextProps);
this.setState(nextState);
if (!deepEquals(this.props.schema, nextProps.schema)) {
if (this.props.onChange) {
this.props.onChange(nextState);
}
}
} The problem with this is that unless the formData prop is cleared on the form before the schema prop is changed, it will have no effect on the internal formData state of the rjsf form. This is because |
Regarding One suggestion in the docs is that we update state in the component constructor. Maybe we should do the calls to |
Moving the I think the correct method to use to replace For now A bigger piece of work will be to upgrade the library to a newer version of react - a lot of the components may need to be changed. For now they are supporting the legacy lifecycle methods until version 17 albeit with the UNSAFE_ prefix which we could run this codemod to update the library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks!
I don't think this is the correct solution - I think rjsf should expose a function to clean formData and crash if it receives unclean formData |
Getting an onChange error and endless loop now with my form -- it seems like updating state in the componentWillReceiveProps is the problem. Investigating further now. |
@graingert what do you mean by "clean data"? The problem I was seeing was if you had a form where you were using the onChange prop to store form state outside of rjsf and the schema contained some defaults, then your "external" form state would not reflect the defaults that rjsf sets in it's internal form state on initialisation. |
@ml242 how did your investigations go? Any chance you could create a jsfiddle demonstrating the problem? |
@llamamoray currently rjsf calls onChange with cleaned data immediately during construction. This shouldn't happen |
…til edited You can test this locally here: http://localhost:9966/?selected=swagger-files%2Ftypes.json Search for "default value" and see that it is already in the code sample. This issue luckily got fixed upstream: rjsf-team/react-jsonschema-form#1034 So to bring that in I needed to update my fork of the module. I would love to get off of my fork and get back onto the main release, but there's some outstanding work on my PR which means we can't right now: rjsf-team/react-jsonschema-form#954
…ts in sc… (rjsf-team#1034)" This reverts commit 9813483.
…y defaults in sc… (rjsf-team#1034)"" This reverts commit 7dfcdcc.
Reasons for making this change
This fixes #1033 where schema defaults aren't being set on any model that is being updated using the form
onChange
prop.Checklist
npm run cs-format
on my branch to conform my code to prettier coding style