0

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?

2 Answers 2

1

use checked instead of defaultChecked as input radio props

Sign up to request clarification or add additional context in comments.

3 Comments

Yes that did the trick. And how can I solve the problem if I set the state to null. If I set it now to null my NO button is checked.
do checked={this.props.state?this.props.state:undefined} and checked={this.props.state?!this.props.state:undefined}.
Yes. Great. Thanks mate.
0

I believe that your radio buttons are not being checked as React is expecting a click event (see https://facebook.github.io/react/docs/forms.html#potential-issues-with-checkboxes-and-radio-buttons), according to that link you'll need to remove the call to preventDefault. Your syntax also appears incorrect to me -

This is a prop not state (If this is correct I'd suggest using a different property name) defaultChecked={this.props.state}

It should look something like this defaultChecked={this.state.xxxx}

With regards to the setting of state, you can use getInitialState to set the state to null, this can then be updated onChange or using a click method.

Your use of defaultChecked appears correct.

1 Comment

I changed defaultChecked to checked and that did the trick. It is in the state, and syntax and everything works fine. I just didn't want to create a big post. And how can I solve the problem if I set the state to null. If I set it now to null my NO button is checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.