【问题标题】:Material-UI , Radio Button + map function ( React JS)Material-UI , 单选按钮 + 地图功能 (React JS)
【发布时间】:2018-01-18 18:08:53
【问题描述】:

所以我需要帮助,我想替换旧的单选按钮(经典) 使用 material-ui 的新版本。我做不到。 请提出解决方案。 提前谢谢。 在图片中你会看到一切

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

【问题讨论】:

  • 对不起,我会把整个代码贴出来
  • var iterator = this.props.questionChoices.map(function(choice){ return (

标签: javascript reactjs material-ui


【解决方案1】:

您也在渲染旧的单选按钮,您还需要在组组件中定义选定的值并只渲染一次组,目前您正在为每个选项渲染它。

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

那么你需要创建handleChange函数来更新状态。

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

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

您可以在这里找到一个工作示例:https://material-ui-next.com/demos/selection-controls/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2021-12-30
    • 2020-08-05
    相关资源
    最近更新 更多