How to toggle boolean state of react component?

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

    Use checked to get the value. During onChange, checked will be true and it will be a type of boolean.

    Hope this helps!

    class A extends React.Component {
      constructor() {
        super()
        this.handleCheckBox = this.handleCheckBox.bind(this)
        this.state = {
          checked: false
        }
      }
      
      handleCheckBox(e) {
        this.setState({
          checked: e.target.checked
        })
      }
      
      render(){
        return 
      }
    }
    
    ReactDOM.render(, document.getElementById('app'))
    
    
    

提交回复
热议问题