-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Update gets triggered on render with initialValues #26
Comments
Did anyone find a work around for this? |
Perhaps you can use the field's Edit: Although I don't see the API able to get meta of the field? |
Interesting. Came across this, because I'm looking for a way to do a initial calculation with the initial values |
Just a thought but is there a chance the form is rendered before the initial values are available and adding them to an existing form could be the culprit? This could be confirmed either with the React profiler tool or logic to prevent render until you have the data for initialValues ready? |
I think that this is valid behavior. I am using exactly this functionality, in order to calculate additional fields, based on initial ones. |
@Dragomir-Ivanov Right, but just because use exploit that fact that it functions like this doesn't mean that it's expected behaviour. I personally would expect an "update" hook to run on updates only, not creation/initialisation. I mean, that could easily be handled outside of Final Form entirely. I do think that this is a an issue that would potentially cause a new major release since it might break a lot of existing uses of this plugin. |
@Soundvessel If you look at my sandbox sample in the original post, you'll see that the value is there from the start. |
@AdamGerthel You may be right. For the time being I guess you can fork your own Decorator, |
Is there any way round this? I load my initial values from a DB and this sets off an update which can make the values different from the DB values. |
@cordial, I got to work around this by wrapping createDecorator function in my own really small decorator that returns the original decorator called with form as its argument. In the updates property you can now access form and check if it is pristine. If it is, simply return an empty object :) Instead of writing const myDecorator = createDecorator({
field: 'fieldA',
updates: {
b: (fieldAValue, allValues) => {
//your calculation
}
}
}); you can write const myDecorator = form => createDecorator({
field: 'fieldA',
updates: form.getState().pristine? {} : {
b: (fieldAValue, allValues) => {
//your calculation
}
}
})(form) |
How can you get access to the form state, when you need to set decorator to render form, and only inside form render callback you can get form state. |
According to the official docs, a decorator is a function that receives the form api as a parameter, subscribes to it, making changes as the state changes, and returns an Unsubscribe function. So when you create a decorator using the createDecorator function, that function call is returning another function in the shape of |
Sorry for necroposting. const myDecorator = form => createDecorator({
field: 'fieldA',
updates: (fieldAValue, fieldName, allValues, prevAllValues) => {
if (form.getState().pristine) return {}
//your calculation
}
}
})(form) Maybe, it will be useful for somebody. |
Are you submitting a bug report or a feature request?
Not sure if it's a bug or a if it works as intended
What is the current behavior?
Updates are triggered once when then the form is rendered, i.e. even though no changes have been made to the form values.
See https://codesandbox.io/s/0ml4k745x0
What is the expected behavior?
I would expect updates to be triggered only on updates, not by the initial values.
What's your environment?
The text was updated successfully, but these errors were encountered: