无条件地在React中声明状态?

问题描述

我正在尝试使用setState访问和更改React Component下的状态。我以某种方式收到错误'iscreateor' is not defined,但已在我的状态下对其进行了定义。这可能是什么错误?预先感谢!

我处于状态的班级组件:

state = {
        training: {},iscreator: false
    }

componentDidMount() {
        if (this.props.token == this.state.training.coach) {
                this.setState({
                    iscreator: true
                });
                console.log(iscreator)
            }else{}
    }

解决方法

您需要引用console.log(this.state.iscreator)

,

library(tidyverse) tab2_newformat <- tab2 %>% pivot_longer(cols = -1) write_csv(tab2_newformat,"tab2.csv",col_names = F) 而不是console.log(this.state.iscreator)

OR

在状态中声明一个引用console.log(iscreator)的常量,例如:

iscreator

然后您可以编写const {iscreator} = this.state;,它将起作用。