问题描述
有没有一种方法可以在Formik中的点击处理程序中获取字段的值?
Is there a way to get the value of a field inside a click handler in formik?
您可以在其中使用setFieldValue
,因此我认为(但在任何地方都找不到)Formik应该具有类似的东西来检索值:
You can use setFieldValue
in there, so I'd assume (but can't find anywhere) that Formik should have something like that for retrieving values:
<Button onClick={() => getFieldValue('name') === 'Test' ? action1 : action2}
在Formik中执行此操作的正确方法是什么?
What is the correct way to do this in Formik?
推荐答案
Formik通过props
将其values
对象传递到您的表单中.假设您有一个输入,以名称firstName
连接到Formik.您可以通过this.props.values.firstName
访问输入值:
Formik passes its values
object into your form via props
. Imagine you have an input, wired into Formik under the name firstName
. You can access the input's value via this.props.values.firstName
:
<button onClick={() => console.log(this.props.values.firstName)}>
Log firstName
</button>
我已经对此进行了测试并验证. 文档中的多个地方也对此进行了演示.
I've test this and verified. It's also demonstrated in several places in the documentation.
这篇关于FormField中的getFieldValue或类似内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!