How to toggle boolean state of react component?

前端 未结 9 1727
别跟我提以往
别跟我提以往 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:21

    You should use this.state.check instead of check.value here:

    this.setState({check: !this.state.check})
    

    But anyway it is bad practice to do it this way. Much better to move it to separate method and don't write callbacks directly in markup.

提交回复
热议问题