Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update specific form field in formik when value changes in redux store?

Tags:

reactjs

formik

I am using formik in my react application. I have initialized all the form values from a state. But I wanted to update a specific form field if props in the redux store change. Below is my formik form:

<Formik
initialValues={this.state.branchData}
enableReinitialize={true}
onSubmit={this.formSubmit} >
{({ values, setFieldValue }) => (
    <Form >
        <Row>
            <Col lg="12">

                <FormGroup className="col-6">
                    <Input type="select" value={values.StateId} name="StateId" onChange={this.handleChange(setFieldValue)}>
                        <option value='' >Select State</option>
                        {this.props.stateList && this.props.stateList.map((item, i) => (
                            <option key={i} value={item.StateId}>
                                {item.StateName}
                            </option>
                        ))}
                    </Input>
                    {this.validator.message('state', values.StateId, 'required')}
                    <Label className="impo_label">Select State</Label>
                </FormGroup>

                <FormGroup className="col-6">
                    <Field className="form-control" name="GstNo" type="text" maxLength="15" />
                    <Label className="impo_label">GST No</Label>
                    {this.validator.message('gst no', values.GstNo, 'required|min:15|max:15')}
                </FormGroup>


            </Col>
        </Row>
    </Form>
)}

Now, When I change state from the dropdown, an api will get invoke which will return gst no by state id. I want to update the gst no field with the value recieved from the api in the props. If the gst no received is null then I want user to input the gst no, but if gst no is recieved from the api, I want to update the gst no field in form with the value received in props and disable the form input. I cannot update the gstno in this.state.branchData as this will reset the form values with values in this.state.branchData.

Does anyone have any idea about how to achieve this in formik?

like image 454
Sunny Avatar asked Feb 24 '20 12:02

Sunny


People also ask

How to access setfieldvalue() outside formik form?

When data comes from api. You can just easy change value of field like this: setFieldValue () is not accessible outside the formik form. Do you have any way through which I can access setFieldValue () outside formik form somewhere in componenetWillRecieveProps ()? you can call function inside formik and call it.

How does formik work with custom components?

Formik will automagically inject onChange, onBlur, name, and value props of the field designated by the name prop to the (custom) component. children can either be an array of elements (e.g. <option> in the case of <Field as="select">) or a callback function (a.k.a render prop). The render props are an object containing:

Is it possible to update a field in a fomik form?

Considering that my Fomik Form has "enableReinitialize", the field update works fine. However, if the form is in "dirty" state, which means, some other fields in the form were updated, this process to update a field does not work anymore.

How do you render a field in formik?

It uses the name attribute to match up with Formik state. <Field /> will default to an HTML <input /> element. There are a few different ways to render things with <Field>. <Field render> deprecated in 2.x. Using these will log warning as can either be a React component or the name of an HTML element to render.


2 Answers

You can enable enableReinitialize property of Formik to update the field.

<Formik
   enableReinitialize={true}
   initialValues={...}
>
....
like image 127
MsiLucifer Avatar answered Oct 08 '22 02:10

MsiLucifer


When data comes from api. You can just easy change value of field like this:

setFieldValue('GstNo', valueFromApi)
like image 1
aturan23 Avatar answered Oct 08 '22 03:10

aturan23