How to pass state values to initial values in formik in react js
Adding enableReinitialize to formik solved my issue
<Formik
enableReinitialize
..
render={
({
..
}) => (
//form uses initialValues
)} />
Answer credit to Abhiram, but in case you have updated to hooks:
useFormik({
initialValues: initialData,
enableReinitialize: true,
onSubmit: values => {
// Do something
}
});
You can imperatively set each value at initialValues prop:
<Formik
initialValues={
formValue1: this.state.value1,
formValue2: this.state.value2,
...
}
validate={validate(validationSchema)}
onSubmit={onSubmit}
render={
({
values,
errors,
touched,
status,
dirty,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
isValid,
handleReset,
setTouched
}) => (
//form uses initialValues
)
}
/>