1

so i need help guys, i want to replace the old radio button (classic) with a new one using material-ui. i couldn't make it. please suggest a solution. thx in advance. in pic u will see everything

class Question extends Component {
  render() {
    var that = this; // Funny way to be able to access this from inside our iterator() function below.
    var iterator = this.props.questionChoices.map(function(choice){
            return (
                <div className="radio">
                <label>
                    <input type= {that.props.questionType} name={that.props.questionID} value={choice}/>
                    {choice}
                </label>
                <RadioButtonGroup >
                    <RadioButton
                        value="ludicrous"
                        label={choice}
                        valueSelected={choice}
                        style={styles.radioButton}
                    />
                </RadioButtonGroup>
                </div>
              );
      });
    return (

            <div className="row">
                <div className="form">
                  <Paper zDepth={1} >
                  <div className="h3">
                    <h3 >{this.props.questionID} - {this.props.questionText}</h3>
                        {iterator}
                        </div>
                        </Paper>
                </div>
            </div>
    );
    }

}

result of the problem image

2
  • sorry here i will post the whole code Commented Jan 18, 2018 at 18:10
  • var iterator = this.props.questionChoices.map(function(choice){ return ( <div className="radio"> <label> <input type= {that.props.questionType} name={that.props.questionID} value={choice}/> {choice} </label><RadioButtonGroup > <RadioButton value="ludicrous" label={choice} valueSelected={choice} style={styles.radioButton} /> </RadioButtonGroup> Commented Jan 18, 2018 at 18:11

1 Answer 1

1

You are rendering the old radio button as well, you also need to define the selected value in the group component and render the group only once, currently you are rendering it for every option.

var iterator = (
  <RadioGroup value={this.state.value} onChange={this.handleChange}>
  {  this.props.questionChoices.map(choice =>
        <FormControlLabel value={choise} control={<Radio />} label={choise} />
    )
  }
  </RadioGroup>
);

Then you need to create the handleChange function to update the state.

class Question extends Component {
  state = {
    value: '',
  };

  handleChange = (event, value) => {
    this.setState({ value });
  };
...

You can find a working example here: https://material-ui-next.com/demos/selection-controls/

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

Comments

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.