How to toggle boolean state of react component?

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

    Depending on your context; this will allow you to update state given the mouseEnter function. Either way, by setting a state value to either true:false you can update that state value given any function by setting it to the opposing value with !this.state.variable

    state = {
      hover: false
    }
    
    onMouseEnter = () => {
      this.setState({
        hover: !this.state.hover
      });
    };
    

提交回复
热议问题