I'm trying to create a yes-no radio buttons with react but using only one state. Thus, I have something like this :
<label className="radio-inline">
<input
type="radio"
name={this.props.nameYes}
defaultChecked={this.props.state}
onChange={this.props.onChangeEmailNotifications}
value="yes"/>Yes
</label>
<label className="radio-inline">
<input
type="radio"
name={this.props.nameNo}
defaultChecked={!this.props.state}
onChange={this.props.onChangeEmailNotifications}
value="no"/>No
</label>
The state I pass with a parent component is set to true.
This onChange function is :
changeCheckboxState: function(event){
var chcName = event.target.name;
switch(chcName){
case "chcEmailNotificationsYes":
this.state.user.emailNotifications = event.target.checked;
this.setState({user: this.state.user});
console.log('checkbox ' + chcName + " state je : ", this.state.user.emailNotifications);
break;
case "chcEmailNotificationsNo":
this.state.user.emailNotifications = !event.target.checked;
this.setState({user: this.state.user});
console.log('checkbox ' + chcName + " state je : ", this.state.user.emailNotifications);
break;
default:
console.log("Sad smo u default");
}
}
But those radio buttons aren't working. In console log I see that the state is changed, but it doesn't have effect on the radio buttons.
And is there any way to set the state to null at the beginning, that buttons aren't checked?