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

本秂侑毒 提交于 2020-06-17 22:58:11

问题


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!


回答1:


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}

 />    


来源:https://stackoverflow.com/questions/62205569/formik-how-to-set-input-field-value-from-components-props

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!