Home > Enterprise >  method is not getting called on onChange event in formik react
method is not getting called on onChange event in formik react

Time:03-04

I have to call below method using formik

   const handleChange = async (e:any, values: any) => {
        alert(e.target.value);
        alert(values);
        alert('Method called');
    };

below is formik code.

 <Formik initialValues={formInitialSchema}
                    validationSchema={formValidationSchema}
                    onSubmit={handleSubmit}>
                    <Form>
                       
                            <div className="col-md-4">
                                <label htmlFor="protoColNo">Protocol No</label>
                                <Field
                                    id="protoColNo"
                                    className="form-control"
                                    name="protoColNo"
                                    placeholder="Enter the Protocol No"
                                />
                                <p className="text-danger">
                                    <ErrorMessage name="protoColNo" />
                                </p>
                            </div>

                            <div className="col-md-4">
                                <label htmlFor="activerequests">Active Requests</label>
                                <select
                                name="activeRequest"
                                style={{ display: 'block' }}
                                onChange= {(e)=>handleChange}>
                                <option value="No"  >No </option>
                                <option value="Yes"  >Yes</option>
                                 <option value="All" selected>All </option>          
                            </select>

                                <p className="text-danger">
                                    <ErrorMessage name="activerequests" />
                                </p>
                            </div>
                        </div>
                    </Form>
                </Formik>
  

I have one input filed and one drop down. As soon as user change the value of drop down I need to call handleChange method with the value of input filed and list. but method is not getting called. I dont know what wrong I am doing?

can you please help me with the same?

CodePudding user response:

onChange= {(e)=>handleChange(e)}> //you forgot to call the handleChange

OR

onChange={handleChange}

CodePudding user response:

You need to call the anonymous function inside the onChange event.

Use the following code :

onChange= {(e)=>handleChange(e)}
  • Related