Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formik - Update initial values after API call

I'm getting my inputs dynamically from an API call based on a change in select input, but when I try to add to the initial values of Formik, it always gives me an error ...

Warning: A component is changing an uncontrolled input of type text to be controlled.

And it doesn't help if I set enableReinitialize={true} to Formik.

However, if I generated the inputs from a local JSON or object, the error goes away.

What am I doing wrong here ...

https://codesandbox.io/s/test-dynamic-inputs-with-formik-xr9qg

The form submits fine though.

like image 628
Ruby Avatar asked Oct 02 '19 16:10

Ruby


People also ask

Does Formik need initial values?

Initial field values of the form, Formik will make these values available to render methods component as values . Even if your form is empty by default, you must initialize all fields with initial values otherwise React will throw an error saying that you have changed an input from uncontrolled to controlled.

How do I get Formik values before submitting?

it is very simple just do console. log(formik. values) and you will get all the values without submitting it.

How do you use handleChange in Formik?

Now we must write the handleChange method to update the state with user inputs: handleChange = ({ target }) => { const { formValues } = this. state; formValues[target.name] = target. value; this.


1 Answers

Better use enableReinitialize={true}. This is official Formik API. You can check this issue

like image 105
KletskovG Avatar answered Sep 30 '22 20:09

KletskovG