首页 > 解决方案 > 反应本机设置状态不起作用

问题描述

我正在尝试更新反应本机组件中的状态。但它得到错误,有人可以帮助我。

我正在使用 react-native-cli 版本:2.0.1 react-native 版本:0.55.4

这是我的代码:

import React, { Component } from 'react'

import {
    Button,
    Text,
    View,
} from 'react-native';

export class ToggleButton extends Component {

    state = {
        isDone: false
    };

    onAction() {
        const value = !this.state.isDone;
        this.setState({ isDone: value });

        const newValue = this.state.isDone;
        console.log(newValue);
    }

    render() {
        return (
            <View>
                <Button
                    title="Action"
                    onPress={this.onAction}
                />
            </View>
        )
    }
}

export default ToggleButton;

标签: reactjsreact-native

解决方案


改变

    onPress={this.onAction} 

    onPress={this.onAction.bind(this)}

检查:这个


推荐阅读