FormField中的getFieldValue或类似内容 [英] getFieldValue or similar in Formik

查看:222
本文介绍了FormField中的getFieldValue或类似内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在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或类似内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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