Comments (13)
Interesting.
This does not happen, when you specify the field as a regex.
And, it gets triggered, but it seems that the changes won't be applied to the values.
Came across this, because I'm looking for a way to do a initial calculation with the initial values
from final-form-calculate.
@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)
from final-form-calculate.
Sorry for necroposting.
I recently stumbled across the same problem.
@leonardopolly suggestion didn't work for me in case when updates
is a function, not an object.
So I just put an if statement inside a function, like this
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.
from final-form-calculate.
Did anyone find a work around for this?
from final-form-calculate.
Perhaps you can use the field's pristine
prop to watch for a change on that specific field? Pristine will respect initialValues.
https://github.com/final-form/final-form#pristine-boolean
Edit: Although I don't see the API able to get meta of the field?
from final-form-calculate.
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?
from final-form-calculate.
I think that this is valid behavior. I am using exactly this functionality, in order to calculate additional fields, based on initial ones.
from final-form-calculate.
@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.
from final-form-calculate.
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?
@Soundvessel If you look at my sandbox sample in the original post, you'll see that the value is there from the start.
from final-form-calculate.
@AdamGerthel You may be right. For the time being I guess you can fork your own Decorator, final-form-calculate
is rather small, until this issue is settled.
from final-form-calculate.
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.
from final-form-calculate.
@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.
from final-form-calculate.
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.
https://final-form.org/docs/final-form/types/Decorator
So when you create a decorator using the createDecorator function, that function call is returning another function in the shape of formApi => { // whatever change you told the decorator to make }
from final-form-calculate.
Related Issues (20)
- How to compute using nested fields? HOT 1
- Access previous values HOT 2
- Allow to block field updates on chained updates HOT 2
- Fieldnames as array don't work when nested fields change HOT 1
- React final form calculate makes dirty form in async initalValues
- Final form calculate doesn't work with array fields for the initial calculation HOT 1
- update is not a function HOT 1
- Why not use useEffect and FormSpy? HOT 2
- FieldPattern supports array of regular expressions HOT 2
- Calculated fields got lost when form reset and none of the fields that trigger the calculations is dirty
- Trigger calculalte/updates only onBlur events HOT 1
- field update does not trigger parse
- Provide FormValues for createDecorator HOT 5
- matching pattern
- field matching pattern
- calculate sum of total price
- Version 1.3.2 lacks the type hint updates HOT 4
- If the component re-render, prevValues will be an empty object and the calculated values will the be broken
- Is this package still alive?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from final-form-calculate.