How to set default Checked in checkbox ReactJS?

前端 未结 16 1933
难免孤独
难免孤独 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:36

    Don't make it too hard. First, understand a simple example given below. It will be clear to you. In this case, just after pressing the checkbox, we will grab the value from the state(initially it's false), change it to other value(initially it's true) & set the state accordingly. If the checkbox is pressed for the second time, it will do the same process again. Grabbing the value (now it's true), change it(to false) & then set the state accordingly(now it's false again. The code is shared below.

    Part 1

    state = {
      verified: false
    } // The verified state is now false
    

    Part 2

    verifiedChange = e => {
      // e.preventDefault(); It's not needed
      const { verified } = e.target;
      this.setState({
        verified: !this.state.verified // It will make the default state value(false) at Part 1 to true 
      });
    }; 
    

    Part 3

      

提交回复
热议问题