Jacek Wojcik
Jacek Wojcik

Reputation: 1263

Formik- how to set input field value from component's props?

I am using Formik for my form.
One of the input is supposed to be populated by props value.
How can I do it?
This is my approach:

<input
     type="text"
     id="country"
     readOnly
     value={props.countryCode}
     // {...formik.getFieldProps("country")}
 />    

but it is most likely interfering with Formik and that is why it does not work.
What is the proper solution?

Thank you!

Upvotes: 1

Views: 8071

Answers (1)

Dipesh KC
Dipesh KC

Reputation: 2463

You can inject props value in initialValues.

Set enableReinitialize to true. So Form gets re-populated when props changes.

<Formik
    initialValues={{ country: props.countryCode }}
    enableReinitialize={true}
 />

 <input
     type="text"
     id="country"
     readOnly
     value={values.country}

 />    

Upvotes: 9

Related Questions