Formik-如何从组件的props中设置输入字段的值? [英] Formik- how to set input field value from component's props?

查看:293
本文介绍了Formik-如何从组件的props中设置输入字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中使用Formik.
输入之一应该由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")}
 />    

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

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

谢谢!

推荐答案

您可以将属性值插入initialValues.

You can inject props value in initialValues.

将enableReinitialize设置为true.因此,当道具发生变化时,Form会重新填充.

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

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

 />    

这篇关于Formik-如何从组件的props中设置输入字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆