How to set default Checked in checkbox ReactJS?

前端 未结 16 1971
难免孤独
难免孤独 2020-11-27 11:51

I\'m having trouble to update the checkbox state after it\'s assigned with default value checked="checked" in React.

var rCheck = React         


        
16条回答
  •  日久生厌
    2020-11-27 12:26

    I tried to accomplish this using Class component: you can view the message for the same

    .....

    class Checkbox extends React.Component{
    constructor(props){
        super(props)
        this.state={
            checked:true
        }
        this.handleCheck=this.handleCheck.bind(this)
    }
    
    handleCheck(){
        this.setState({
            checked:!this.state.checked
        })
    }
    
    render(){
        var msg=" "
        if(this.state.checked){
            msg="checked!"
        }else{
            msg="not checked!"
        }
        return(
            

    this box is {msg}

    ) }

    }

提交回复
热议问题