Change radio switch do not update

Attempting to react to the radio, but strange that selecting the radio won't update its look. Can anyone take a look at the below code and let me know what happened?

  class Test extends React.Component {
    constructor(props){
    super(props);

    this.state = {
      radioVal: "first"
    }
  }

  handleRadioSelection(e){
    e.preventDefault();

    this.setState({
      radioVal:e.target.value
    })
  }

    render(){
      return (
        <div>
            <label>
            <input type="radio" onChange={this.handleRadioSelection.bind(this)} 
            checked={ this.state.radioVal==="first"} 
            value="first" 
            name="radio1" /> 1 <br />
          </label>

          <label>
            <input type="radio" onChange={this.handleRadioSelection.bind(this)} 
            checked={this.state.radioVal==="second"} 
            value="second" 
            name="radio1" /> 2 <br />
          </label>
            { "Selected: " } {this.state.radioVal}
        </div>
    )
  }
}

React.render(<Test />, document.getElementById('container'));

      

+3


source to share


1 answer


Remove e.preventDefault();

from handleRadioSelection

. Is there any reason you added this line in the first place?



+1


source







All Articles