Javascript React Native中始终未定义Ref值
我不明白为什么我的ref值总是未定义。我知道这类问题已经发布了,但没有任何帮助 这是我的密码:Javascript React Native中始终未定义Ref值,javascript,react-native,undefined,ref,Javascript,React Native,Undefined,Ref,我不明白为什么我的ref值总是未定义。我知道这类问题已经发布了,但没有任何帮助 这是我的密码: class CallsHandler extends React.Component { constructor(props) { super(props); this.handleRegistration = this.handleRegistration.bind(this); this.usernameInput = React.creat
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>
)
}
}
class CallsHandler扩展React.Component{
建造师(道具){
超级(道具);
this.handleRegistration=this.handleRegistration.bind(this);
this.usernameInput=React.createRef();
}
HandlerRegistration(){
log(this.usernameInput.current.value);
}
渲染(){
返回(
用户名
)
}
}
您必须将方法绑定到组件上下文
onPress={this.handleRegistration.bind(this)}试试这个:
<TextInput ref={input => (this.nameInput = input)}></TextInput>
(this.nameInput=input)}>
同时删除这一行
this.usernameInput=React.createRef()代码>谢谢,但它不会改变什么