Search code examples
reactjsformik

formik validation schema & initial values are not updating


formKey's state is getting changed in a child component, formKey's value is up to date in the console but the formik is not taking the updated values.

<Formik
          initialValues={formKey===1? initialValues: formKey===2? profileUrlsInitialValues: csvFileInitialValues}
          onSubmit={() => {}}
          validationSchema={formKey===1? addLeadsValidationSchema: formKey===2? profileUrlsValidationSchema: uploadCsvValidationSchema}
          validateOnMount
        >

i have tried using switch case and passing one function but it is not working, creating form's based on formKey value is not a good practice in code writing so is there something from which i can update the values?


Solution

  • There is a prop for that, you can use enableReinitialize

    https://formik.org/docs/api/formik#enablereinitialize-boolean

    If initialValues changes its value on next render, it will reset the form. The values are not copared by reference, it uses deep equality