How to toggle boolean state of react component?

前端 未结 9 1830
别跟我提以往
别跟我提以往 2021-01-29 23:45

I\'d like to know how to toggle a boolean state of a react component. For instance:

I have boolean state check in the constructor of my component:

const         


        
9条回答
  •  轮回少年
    2021-01-30 00:24

    I found this the most simple when toggling boolean values. Simply put if the value is already true then it sets it to false and vice versa. Beware of undefined errors, make sure your property was defined before executing

    this.setState({
       propertyName: this.propertyName = !this.propertyName
    });
    

提交回复
热议问题