React native 从未使用状态的引用获取文本值
我试图使用引用从TextInput获取一个值,但它总是打印未定义。我不知道为什么会这样React native 从未使用状态的引用获取文本值,react-native,React Native,我试图使用引用从TextInput获取一个值,但它总是打印未定义。我不知道为什么会这样 <TextInput ref={value =>this._passwordRef = value } style={styles.inputTextStyle} placeholder='Enter password here'> </TextInput> <TouchableOpacity onPress={(view)=>{th
<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);
}
this.\u passwordRef=value}
style={style.inputExtStyle}占位符=“在此处输入密码”>
{this.onSubmit()}style={{{height:50,borderColor:'#000',borderWidth:2,marginLeft:20,marginRight:20}>
提交
onSubmit(){
console.log(this.\u passwordRef.\u lastNativeText);
}
react的当前版本为
“反应”:“16.13.1”,
“react-native”:“0.63.3”根据react-native文档,文本输入似乎没有ref-props 您需要使用
onChangeText
和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;
import React,{Component}来自'React';
从“react native”导入{TextInput};
常量textInputT=()=>{
const[value,onChangeText]=React.useState(“无用占位符”);
返回(
onChangeText(文本)}
value={value}
/>
);
}
导出默认文本输入;