Formik - Update initial values after API call
Asked Answered
R

4

14

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.

Retaliate answered 2/10, 2019 at 16:52 Comment(0)
R
9

If anyone is facing the same issue, I just found the solution ...

You have to set value={field.value || ''} in the input inside the TextInput component or whatever type you're using in order to fix this issue.

Retaliate answered 2/10, 2019 at 18:39 Comment(1)
Thanks for that! I've been looking for this answer for a while.Calciferous
S
27

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

Swordcraft answered 18/3, 2021 at 15:59 Comment(1)
This saved a lot of timeCerium
R
9

If anyone is facing the same issue, I just found the solution ...

You have to set value={field.value || ''} in the input inside the TextInput component or whatever type you're using in order to fix this issue.

Retaliate answered 2/10, 2019 at 18:39 Comment(1)
Thanks for that! I've been looking for this answer for a while.Calciferous
O
2

I had a complex, dynamic form and also came across this issue. There are a few things that I'd recommend for anyone debugging this issue in the future:

  1. Set value for your Field component--like Ruby does above.
  2. Ensure that your Formik component has a uniquely identifying key.
  3. Track and debug your initialValues and ensure that all fields are accounted for. You shouldn't have to set the field value like Ruby does--so long as your initialValues object accounts for all of the fields. However, my form dynamically changed Field components--and Ruby's solution is the only one that worked.

If your form is not dynamic--I think it might be best to check your initialValues object first before implementing Ruby's solution. Formik should be taking care of those values for you--which is why it's such an awesome tool.

Official answered 4/4, 2020 at 3:18 Comment(2)
I agree. If the form is not dynamic, there's no need for this at all.Retaliate
After the form re-rendered, I was getting old input values that didn't match Formik's current state. Your #2 bullet above saved me. For those who have a similar issue, put a random key on your input to ensure React doesn't just re-use the old value. This is especially valuable on dynamic form fields that are constantly being refreshed due to other inputs.Malca
B
1

i've checked with enableReinitialize={true}. But its not working as much as expected. so wrote a useEffect like

 useEffect(() => {
        formik.setFieldValue('query_string', active?.query);
    }, [active])

it's worked !

Backswept answered 17/1, 2023 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.