javascript - React Native 中的 Ref 值始终未定义
问题描述
我不明白为什么我的 ref 的值总是未定义的。我知道这种问题已经发布,但没有任何帮助。
这是我的代码:
class CallsHandler extends React.Component {
constructor(props) {
super(props);
this.handleRegistration = this.handleRegistration.bind(this);
this.usernameInput = React.createRef();
}
handleRegistration() {
console.log(this.usernameInput.current.value);
}
render () {
return (
<View>
<Text>Username</Text>
<TextInput ref={this.usernameInput}></TextInput>
<Button title="REGISTER" onPress={this.handleRegistration}/>
</View>
)
}
}
解决方案
您必须将您的方法绑定到组件上下文
onPress={this.handleRegistration.bind(this)}
推荐阅读
- python - python中特定时间的提醒
- visual-studio-code - Visual Studio Code 中的自定义 autoClosingBrackets
- javascript - JSON.parse 带有转义的字符串
- reactjs - 当存在两个相同的图标时,只会显示一个
- c++ - 为什么当我输入与数组中的号码匹配的收费号码时我的程序无法运行
- azure-devops - Azure Pipelines:使用外部“包含目录”构建 C++ 项目
- javascript - 通过鼠标多方向javascript水平滚动
- numpy - Model.predict 抛出 TypeError:'numpy.ndarray' 对象不可调用
- excel - 按类别计算非空白值
- git - 强制分支成为主人