React Formik updating one field based on another
See original GitHub issue❓Question
I’m using formik react library and trying to update 2 fields, based on the onChange event of anothers. For example,
price = quantity * totalPrice
price :
onChange={() => {
setFieldValue('quantity',values.quantity? values.price / values.totalPrice:values.quantity, );
setFieldValue('totalPrice',values.totalPrice? values.price * values.quantity: values.totalPrice,);
}
}
quantity :
onChange={(value, e) => {
this.disableFiled(value, e);
setFieldValue('totalPrice',values.price ? values.price * values.totalPrice : ' ',);
}
}
totalPrice:
onChange={(value, e) => {
this.disableFiled(value, e);
setFieldValue('quantity',values.price ? values.totalPrice / price : ' ', );
}
}
when quantity has value total price will be disabled and vice versa.but it doesn’t calculate other fields correctly
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:11
Top Results From Across the Web
React Formik updating one field based on another
I'm using formik react library and trying to update one field, based on the onChange event of another. For example I have one...
Read more >Dependent Fields Example - Formik
This is an example of how to set the value of one field based on the current values of other fields in Formik....
Read more >Validate one field based on another Field in redux Form-Reactjs
The synchronous validation function is given all the values in the form. Therefore, assuming your checkbox is a form value, you have all...
Read more >Formik - struggling to get onChange to work. Just want to ...
Just want to update a field from another field's value. I just want to update the machineName based on what's entered into the...
Read more >useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can also use an effect and a child component. Theres been some discussion of change listeners but we haven’t found the ideal API for it yet.
In child component: (apologies for mobile formatting)
Pros are a more decentralized approach, cons are extra components and a render where they aren’t in sync.
I’d recommend against changing properties directly on formik.values! Treat it as an immutable object.
@rahi-nz Could you kindly format your code? It is very hard to read.
I stumbled upon this issue when researching whether there is a more elegant solution than ours. This is what we did (pseudo-code):
I find this rather disturbing, but we could not think of a better way to do it. Can you, @jaredpalmer?
I find the above disturbing because
e.target.value
)