【问题标题】:react radio button doesn't work on the first click反应单选按钮在第一次点击时不起作用
【发布时间】:2020-06-25 03:29:30
【问题描述】:

我有一个使用纯 css 的单选按钮,但它在第一次点击时不起作用,它只在第二次点击时起作用,不确定它是否与我的 react 道具有关:

const Radio = ({ id, name, value, checked, children }) => (
  <div className="radioBtn">
    <input type="radio" value={value} id={id} name={name} checked={checked} />
    <label className={"radio"} htmlFor={id}>
      <span className={"big"}>
        <span className={"small"} />
      </span>
      <span>{children}</span>
    </label>
  </div>
);

https://codesandbox.io/s/react-sass-34b8w

【问题讨论】:

  • 你读错了吗:Warning: Failed prop type: You provided a 'checked' prop to a form field without an 'onChange' handler. This will render a read-only field. If the field should be mutable use 'defaultChecked'. Otherwise, set either 'onChange' or 'readOnly'?

标签: javascript css reactjs sass


【解决方案1】:

使用defaultChecked 而不是checked={checked}

【讨论】:

    猜你喜欢
    • 2015-03-21
    • 2016-07-14
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多