【问题标题】:Dynamic changing style of checked radio button in ReactReact中选中单选按钮的动态变化样式
【发布时间】:2018-12-13 22:37:02
【问题描述】:

我有 7 个按钮单选类型。每个按钮都有不同的样式,并且在checked=true 时必须看起来不同。我希望他们在检查时改变他们的风格。 这是我负责创建单按钮单选外观的组件

class RadioButton extends Component {
  constructor() {
    super();
    this.state = {
      checked: false,
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange() {
    const { checked } = this.state;
    this.setState({
      checked: !checked,
    });
  }

  render() {
    const {
      value,
      name,
      backgroundColor,
      backgroundColorChecked,
      borderColor,
      height,
      width,
      ...otherProps
    } = this.props;
    const { checked } = this.state;
    return (
      <InputGroup>
        <Input
          name={name}
          value={value}
          checked={checked}
          onChange={this.handleChange}
          style={{
            backgroundColor: checked ? `${backgroundColorChecked}` : `${backgroundColor}`,
            borderColor: `${borderColor}`,
            height: `${height}`,
            width: `${width}`,
          }}
          {...otherProps}
        />
      </InputGroup>
    );
  }
}

这是我负责创建几个按钮单选的组件

 <FinalField name="rate">
          {({ input }) => (
            <FormGroup>
              <RadioButton
                value="capable"
                type="radio"
                backgroundColorChecked="#6baf8f"
                backgroundColor="#f5fffa"
                borderColor="#6baf8f"
                height="1.8125rem"
                width="1.8125rem"
                {...input}
              />
            </FormGroup>
          )}
        </FinalField>

在控制台中我收到两个错误You must pass 'type="radio"' prop to your Field(rate) component. Without it we don't know how to unpack your 'value' prop - "undefined". 和Failed prop type: The prop 'style' is marked as required in 'RadioButton', but its value is 'undefined'。

我不知道为什么会出现这些错误以及如何修复它们。感谢所有提示。

【问题讨论】:

    标签: reactjs radio-button


    【解决方案1】:

    您必须将 'type="radio"' 属性传递给您的 Field(rate) 组件。

    这个问题意味着您需要在 RadioButton 类的输入元素中添加 'type="radio"'。

    <Input
              type="radio"
              name={name}
              value={value}
              checked={checked}
              onChange={this.handleChange}
              style={{
                backgroundColor: checked ? `${backgroundColorChecked}` : `${backgroundColor}`,
                borderColor: `${borderColor}`,
                height: `${height}`,
                width: `${width}`,
              }}
              {...otherProps}
            />
    

    失败的道具类型:

    我假设您在 RadioButton 类中定义了 propType,如下所示:

    RadioButton.protoTypes = {
        style: ProtoTypes.object.isRequired
    }
    

    但是,样式属性没有传递给 RadioButton 类。

    【讨论】:

    • 不,那个 RadioButton.propTypes = { value: PropTypes.string.isRequired, name: PropTypes.string.isRequired, style: PropTypes.shape({ backgroundColor: PropTypes.string.isRequired, borderColor: PropTypes .string.isRequired,高度:PropTypes.string.isRequired,宽度:PropTypes.string.isRequired,}).isRequired,};
    • 您已经在 RadioButton.propTypes 中定义了样式。但是它没有传入
    • 解决了一个问题-道具类型:道具'style'在'RadioButton'中被标记为必填,但它的值是'undefined'
    • 从哪里获取您的 Input 元素?应该输入原始输入元素而不是 Input。
    • 我有组件 QuestionsList.js 并获取数据并将其作为 props QuestionsListItem.js 传输,单选按钮传输到此组件
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 2018-09-07
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多