How to get the value of a group of Radio Buttons in ReactJS

Birish

I have a group of 4 radio buttons and I need to get the value of the selected button. I should make sure that the user has selected one of the radio buttons. Here is how I'm trying to do it:

export const ReportClass = React.createClass({
    mixins: [React.addons.LinkedStateMixin],

    getInitialState() {
        return {
            reason: "",
        }
    },

    setError(id, msg) {
        const err = this.state.errors;
        err[id] = msg;
        this.setState({errors: this.state.errors});
    },

    sendReport(event) {
        event.preventDefault();
        console.log(this.state.reason);
        console.log(this.state.reason.value);
        if (!this.state.reason) {
           this.setError('reason', "Please choose one");
           return;
        }
    },

    render: function() {
        return (
            <div>
                <form className="form-h" onSubmit={this.sendReport}>                        
                    <label forHtml='first'> first radio button </label> 
                    <input ref='first' name='reason' type='radio' value='first' checkedLink={this.linkState('reason')} />

                    <label forHtml='second'> second radio button </label>
                    <input ref='second' name='reason' type='radio' value='second' checkedLink={this.linkState('reason')} />

                    <label forHtml='third'> third radio button</label>
                    <input ref='third' name='reason' type='radio' value='third' checkedLink={this.linkState('reason')} />

                    <label forHtml='forth'> forth radio button </label>
                    <input ref='forth' name='reason' type='radio' value='forth' checkedLink={this.linkState('reason')} />

                    <div className="col-sm">
                        <button type="submit" className="btn" onClick={this.sendReport}> Send</button>
                    </div>                            
                </form>
            </div>
        );
    }
});

The console.log(this.state.reason) returns true when one of the buttons is selected and the second one, console.log(this.state.reason.value) returns undefined. So how can I get the value to know which one is chosed?

Alex Guerra

I think that checkedLink binds checked-ness of an individual radio button to the state. Because you're binding multiple radio buttons to the same state key and radio buttons are mutually exclusive, the reason will always be whichever change event fired last.

In my opinion, the best solution is to use react-radio-group.

If you don't want an external dependency, an easy alternative is to not link state at all and set an onChange handler on every radio button that looks like this:

function onChange(e) {
  if (e.target.selected) {
    this.setState({ reason: e.target.value });
  }
}

Now we don't need to worry about managing the checked-ness of each radio button, we just grab the reason whenever one is clicked. The downside to this is that it doesn't go both ways: if you set the reason on the state it wouldn't change which radio button is actually clicked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery get value of group of radio buttons

From Dev

jquery get value of group of radio buttons

From Dev

How to get data from radio buttons in ReactJS?

From Java

How to use radio buttons in ReactJS?

From Dev

How to get the value of radio buttons so that the value can be manipulated

From Dev

ReactJS: How to use boolean values in radio buttons?

From Dev

How to associate a label with a group of radio buttons?

From Dev

How to disable group of radio buttons in java

From Dev

How to manually group Radio Buttons Rails 4

From Dev

How do validate a group of radio buttons?

From Dev

How to manage events for a series of group of radio buttons?

From Dev

How to make a group of radio buttons from numbers

From Dev

Get the selected value of a list of radio buttons

From Dev

Get bootstrap radio value from href buttons

From Dev

How to get a value of radio button in the option group Access VBA

From Dev

android how to align radio-buttons inside radio group?

From Dev

How can I get Parsley.js to oupt 1 error for a group of radio buttons or checkboxes?

From Dev

Disable group of radio buttons

From Dev

Radio buttons group

From Dev

Put radio buttons in group

From Dev

How will I populate value in textarea and radio buttons

From Dev

how to uncheck a group of radio buttons when one in another group is checked

From Dev

how to get the values of list of radio buttons in jsp

From Dev

How to get data from radio buttons? angularjs

From Dev

How to get a Div to move randomly with radio buttons?

From Dev

How to get the radio buttons that are NOT checked (Jquery)?

From Dev

Changing value attribute of radio buttons in a group using jQuery

From Dev

How to clear radio buttons / clear all selected radio buttons to false state. reactjs - bootstrap

From Dev

jQuery - get active button from buttons group (input type = "radio")

Related Related

  1. 1

    jquery get value of group of radio buttons

  2. 2

    jquery get value of group of radio buttons

  3. 3

    How to get data from radio buttons in ReactJS?

  4. 4

    How to use radio buttons in ReactJS?

  5. 5

    How to get the value of radio buttons so that the value can be manipulated

  6. 6

    ReactJS: How to use boolean values in radio buttons?

  7. 7

    How to associate a label with a group of radio buttons?

  8. 8

    How to disable group of radio buttons in java

  9. 9

    How to manually group Radio Buttons Rails 4

  10. 10

    How do validate a group of radio buttons?

  11. 11

    How to manage events for a series of group of radio buttons?

  12. 12

    How to make a group of radio buttons from numbers

  13. 13

    Get the selected value of a list of radio buttons

  14. 14

    Get bootstrap radio value from href buttons

  15. 15

    How to get a value of radio button in the option group Access VBA

  16. 16

    android how to align radio-buttons inside radio group?

  17. 17

    How can I get Parsley.js to oupt 1 error for a group of radio buttons or checkboxes?

  18. 18

    Disable group of radio buttons

  19. 19

    Radio buttons group

  20. 20

    Put radio buttons in group

  21. 21

    How will I populate value in textarea and radio buttons

  22. 22

    how to uncheck a group of radio buttons when one in another group is checked

  23. 23

    how to get the values of list of radio buttons in jsp

  24. 24

    How to get data from radio buttons? angularjs

  25. 25

    How to get a Div to move randomly with radio buttons?

  26. 26

    How to get the radio buttons that are NOT checked (Jquery)?

  27. 27

    Changing value attribute of radio buttons in a group using jQuery

  28. 28

    How to clear radio buttons / clear all selected radio buttons to false state. reactjs - bootstrap

  29. 29

    jQuery - get active button from buttons group (input type = "radio")

HotTag

Archive