我刚刚开始学习反应,我的输入字段有问题。我没有任何编译错误,但是当我执行并按下按钮时,我有一个错误:
未定义不是对象('this.state.username'(
这可能与国家管理不善有关。
这是我的代码:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TextInput,
Button
} from 'react-native';
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
username: '',
password: ''
};
};
_userSignup() {
const username = this.state.username;
const password = this.state.password;
}
render() {
return (
<TextInput
onChangeText={(text) => this.setState({username:text})}
value={this.state.username}
placeholder = "Username"
/>
<TextInput
onChangeText={(password) => this.setState({password})}
value={this.state.password}
placeholder = "Password"
/>
<Button
title="sign in"
onPress={this._userSignup}
/>
</View>
);
}
}
谢谢您的帮助。
您要么这样做
_userSignup() {
const { username } = this.state;
const { password } = this.state;
}
或这样做
_userSignup() {
const username = this.state.username;
const password = this.state.password;
}
了解有关ES6破坏功能的更多信息或查看Dan对此的评价。