question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 4 years ago
  • Reactions:7
  • Comments:11

github_iconTop GitHub Comments

9reactions
johnromcommented, Jan 30, 2020

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)

const formik = useFormikContext();

React.useEffect(() =>
    formik.setFieldValue("dependentField", calculateDependentValue(formik.values.mainField);
}, [formik.values.mainField]);

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.

4reactions
alexbepplecommented, Oct 1, 2019

@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):

onChange={(e) => {
    formikProps.handleChange(e);
    formikProps.values.dependentValue = calcDependent(e.target.value);
}}

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

  • it throws the abstraction that Formik provides out of the window (e.target.value)
  • it modifies the state of something that does not have a specified API. Not only is this hacky, but it might also stop working with any release.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found