S
S
sharkdest2019-03-09 18:15:04
JavaScript
sharkdest, 2019-03-09 18:15:04

How to change the value of another field in Formik?

Hello, please tell me how to change the value of another field in onChange () of the first field?
I'll show you with an example:

<form onSubmit={handleSubmit}>
  <Typeahead
    name="firstField"
    onChange={firstField => {
      setFieldValue("firstField", firstField);
    }}
    onInputChange={firstField => {
      setFieldValue("firstField", firstField);
    }}
    options={data}
    labelKey="key"
  />

  <Typeahead
    name="secondField"
    onChange={secondField => {
      setFieldValue({
        secondField: secondField, //текущее поле менят свое значение
        firstField: "testValue" //при этом первое не меняется
      });
    }}
    onInputChange={secondField => {
      setFieldValue("secondField", secondField);
    }}
    options={data}
    labelKey="key"
  />
</form>

Thanks in advance for your reply.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question