【问题标题】:Formik- how to set input field value from component's props?Formik-如何从组件的道具中设置输入字段值?
【发布时间】:2020-06-05 12:34:51
【问题描述】:

我在表单中使用Formik
其中一个输入应该由props 值填充。
我该怎么做?
这是我的方法:

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

但它很可能会干扰Formik,这就是它不起作用的原因。
什么是正确的解决方案?

谢谢!

【问题讨论】:

  • 您是否将其导入另一个组件并添加到

标签: reactjs formik


【解决方案1】:

您可以在 initialValues 中注入 props 值。

将 enableReinitialize 设置为 true。所以当 props 改变时 Form 会被重新填充。

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

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

 />    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 1970-01-01
    • 2020-08-04
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多