在 React Native 中获取 TextInput 值和复选框状态
Get TextInput Value and Checkbox state in React native
我想检索用户名和密码的输入值以及复选框状态。我在 Whosebug 上找到了这个相关问题。但是当尝试实现这个时出现错误
'null' is not an object (evaluating 'this.state.username')
谁能帮我检索输入值和复选框状态,因为我是 React-native 的新手
代码:
var CheckBox = require('react-native-checkbox');
var Button = require('react-native-button');
var reactnative = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.signin}>Sign In</Text>
<TextInput style={styles.logininput} ref= "username" onChangeText={(event) => this.setState({username:event.nativeEvent.text})} value={this.state.username}/>
<TextInput style={styles.logininput} value="Password"/>
<CheckBox label='Remeber Me' checked = {false} onClick={this._CheckBoxState}/>
<Button style={styles.loginButton} onPress={this._handlePress.bind(this)}>
Login
</Button>
</View>
);
},
_handlePress(event) {
//Get TextInput value
var username= this.state.username;
alert(username);
},
_CheckBoxState(event){
//want to change the state from true to false or vice-versa
}
});
您可能错过了在渲染前初始化初始状态函数。
...
getInitialState: function() {
return {username: ''};
},
render: function() {
....
}
现在 this.state.username 应该可以访问了。
我想检索用户名和密码的输入值以及复选框状态。我在 Whosebug 上找到了这个相关问题。但是当尝试实现这个时出现错误
'null' is not an object (evaluating 'this.state.username')
谁能帮我检索输入值和复选框状态,因为我是 React-native 的新手
代码:
var CheckBox = require('react-native-checkbox');
var Button = require('react-native-button');
var reactnative = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.signin}>Sign In</Text>
<TextInput style={styles.logininput} ref= "username" onChangeText={(event) => this.setState({username:event.nativeEvent.text})} value={this.state.username}/>
<TextInput style={styles.logininput} value="Password"/>
<CheckBox label='Remeber Me' checked = {false} onClick={this._CheckBoxState}/>
<Button style={styles.loginButton} onPress={this._handlePress.bind(this)}>
Login
</Button>
</View>
);
},
_handlePress(event) {
//Get TextInput value
var username= this.state.username;
alert(username);
},
_CheckBoxState(event){
//want to change the state from true to false or vice-versa
}
});
您可能错过了在渲染前初始化初始状态函数。
...
getInitialState: function() {
return {username: ''};
},
render: function() {
....
}
现在 this.state.username 应该可以访问了。