formik , setFieldValue one step behind


I have this problem with formik , I want use setFieldValue and set one of my values to total of my values … and problem is when i pass onChange to my customInput component setFieldValu update with one step behind ! but if i dont pass setFieldValue and i write the customCompontnt in my page it works fine and its not one step behind … so please help me that i understand how to pass my setField value to another component and works fine .

its customInput in my form :

 <CustomInput
                      index={index}
                      name={`formItems.${index}.amount`}
                      title="test"
                      placeholder="test"
                      inputType="text"
                      onChange={async (e) => {
                        handleChange(e)
                        setFieldValue(
                          "totalCount",
                          values.formItems.reduce(
                            (previousValue, currentValue) =>
                              +previousValue + +currentValue.amount,
                            0
                          )
                        );
                      }}
                    />

and its my customInput code (sorry, wen i paste code it dosent show good)

Source: JavaSript – Stack Overflow

November 27, 2021
Category : News
Tags: formik | input | javascript | reactjs

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.