react-native - 从不使用状态的参考中获取文本值
问题描述
我正在尝试使用参考从 TextInput 获取一个值,但它总是打印未定义。我不知道为什么会这样。
<TextInput
ref={value =>this._passwordRef = value }
style={styles.inputTextStyle} placeholder='Enter password here'>
</TextInput>
<TouchableOpacity onPress={(view)=>{this.onSubmit()}} style={{height:50,borderColor:'#000',borderWidth:2,marginLeft:20,marginRight:20}}>
<Text style={styles.btnTextStyle}>Submit</Text>
</TouchableOpacity>
onSubmit(){
console.log(this._passwordRef._lastNativeText);
}
当前版本的反应是。
“反应”:“16.13.1”,“反应原生”:“0.63.3”
解决方案
根据 ReactNative 文档,TextInput 似乎没有参考道具。
onChangeText
您需要使用and来处理更改和检查输入的值value
。检查下面的代码以获得更好的理解
import React, { Component } from 'react';
import { TextInput } from 'react-native';
const TextInputtt = () => {
const [value, onChangeText] = React.useState('Useless Placeholder');
return (
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
/>
);
}
export default TextInputtt;
推荐阅读
- java - 使用 Math.random 随机生成 100 个唯一数字
- jquery - 通过 jquery 在 Boostrap 4 轮播中添加“活动”类,从 mongodb 加载数据时无法正常工作
- vagrant - 如果配置 Vagrant 文件到 forward_port 不起作用怎么办;有什么选择吗?
- .net - Azure 应用设置中的 Key Vault 设置,没有代码
- postgresql - 如何在不多次加入自身的情况下进行数据透视表
- javascript - 如何禁用 $('#bootstrap-table') 中的选定字段
- r - 如何结合 mutate_all 和 ifelse
- android - Android中的Linux套接字连接失败
- android - 关于 Facebook SDK 的密钥哈希的几个问题
- javascript - Foundation 显示模态停止执行类似 alert() 的 javascript;