在 React Native 中按下复选框时切换真假

时间:2020-12-26 14:31:57

标签: reactjs react-native

我正在尝试在 react-native 中创建一个复选框。我想通过单击按钮取消选中它

下面是我的代码:

import { View } from 'react-native';
import { CheckBox } from 'react-native-elements';

export default class SignIn extends React.Component {
    constructor() {
        super();
        this.state = { tick: false, }
    }

    checkTick() {
        this.setState({ tick: !this.state.tick })
    }
    render() {
        return (
            <View style={styles.SignInConfirmation}>
                <CheckBox
                    title='click'
                    checked={this.state.tick}
                    onPress={this.checkTick}
                />
            </View>
        );
    }
}

错误:undefined 不是一个对象(评估 'this.setState')

我不明白问题出在哪里。

1 个答案:

答案 0 :(得分:0)

试试这样

import { View } from 'react-native';
import { CheckBox } from 'react-native-elements';

export default class SignIn extends React.Component {
    constructor() {
        super()
    }
    state = { tick: false, }
    checkTick() {
        this.setState({ tick: !this.state.tick })
    }
    render() {
        return (
            <View style={styles.SignInConfirmation}>
                <CheckBox
                    title='click'
                    checked={this.state.tick}
                    onPress={this.checkTick}
                />
            </View>
        );
    }
}