调用 onBlur 时如何获取 TextInput 中的值? [英] How do I get the value in TextInput when onBlur is called?

查看:504
本文介绍了调用 onBlur 时如何获取 TextInput 中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 React Native 中,我想在 onBlur 事件处理程序中传递 TextInput 的值.

In React Native, I want to pass the value of the TextInput in the onBlur event handler.

onBlur={(e) => this.validateText(e.target.value)}

e.target.value 适用于普通 React.但是,在 react-native 中, e.target.value 是未定义的.React Native 中可用的事件参数的结构是什么?

e.target.value works for plain React. But, in react-native, e.target.value is undefined. What is the structure of event args available in React Native?

推荐答案

在 React Native 中,你可以从 e.nativeEvent.text 获取 TextInput 的值.

In React Native, you can get the value of the TextInput from e.nativeEvent.text.

不幸的是,这不适用于 multiline={true}.解决此问题的一个技巧是维护对 TextInput 的引用,并通过组件的 _lastNativeText 属性访问文本值.例如(假设您已为 TextInput 组件分配了textInput"的引用):

Unfortunately, this doesn't work for multiline={true}. One hack around this is to maintain a ref to your TextInput and access the text value through the _lastNativeText property of the component. For example (assuming you've assigned your TextInput component a ref of "textInput"):

onBlur={() =>console.log(this.refs.textInput._lastNativeText)}

这篇关于调用 onBlur 时如何获取 TextInput 中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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