问题
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