0
我有问题,在不断变化的state.It的值给出英里以下错误 “取消定义不是一个函数(计算‘ this.setState({ switchValue:假 })’)”如何改变本机的状态值?
我已经上搜索堆栈溢出,但解决方案不适合我。
import React, { Component } from 'react';
import {
Alert,
AppRegistry,
StyleSheet,
Text,
View,
Switch
} from 'react-native';
export default class AwesomeProject extends Component {
constructor(props) {
super(props);
this.state = {switchValue: true};
// Toggle the state every second
}
_onPressButton() {
this.setState({
switchValue: false
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Switch
onValueChange={this._onPressButton}
value={this.state.switchValue }
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('AwesomeProject',() => AwesomeProject);
可以绑定你的函数调用像'onValueChange = {this._onPressButton.bind(本)}'因为你'_onPressButton'需要访问'this'在'这.setState({})' – Andy