reactjs 单选按钮组件不更新默认选项

9njqaruj  于 2023-01-04  发布在  React
关注(0)|答案(2)|浏览(140)

有一个单选按钮组可重用组件,该组件应该能够设置所选的默认选项。
以下是组件的调用方式:

const App = () => {
  const drinks = [
    {
      label: "Coffee",
      name: "drinks-list"
    },
    {
      label: "Tea",
      name: "drinks-list"
    },
    {
      label: "Water",
      name: "drinks-list",
      disabled: true
    }
  ];

  const [selectedValue, setSelectedValue] = useState<String>(drinks[1].label);

  function radioGroupHandler(event: React.ChangeEvent<HTMLInputElement>) {
    setSelectedValue(event.target.value);
  }

  useEffect(() => {
    console.log(selectedValue);
  }, [selectedValue]);

  return (
    <PageLayout>
      <RadioButtonGroup
        label="Select your drink:"
        options={drinks}
        onChange={radioGroupHandler}
      />
    </PageLayout>
  );
};

export default App;

有一行用于设置状态:

const [selectedValue, setSelectedValue] = useState<String>(drinks[1].label);

在这种情况下,预期的行为是在呈现组件时检查第二个选项(Tea),但它不是这样工作的-它呈现了第一个选项。如果代码是这样的,我希望呈现第一个选项:

const [selectedValue, setSelectedValue] = useState<String>(drinks[0].label);

这是单选按钮组组件:

const RadioButtonGroup = ({ label, options, onChange }: IInputGroup) => {
  function renderOptions() {
    return options.map(({ label, name, disabled }: IOption, index) => {
      const shortenedOptionLabel = label.replace(/\s+/g, "");
      const optionId = `radio-option-${shortenedOptionLabel}`;

      return (
        <RadioButton
          value={label}
          label={label}
          key={optionId}
          id={optionId}
          name={name}
          disabled={disabled}
          defaultChecked={index === 0}
          onChange={onChange}
        />
      );
    });
  }
  return (
    <Fieldset>
      <Legend>{label}</Legend>
      <Wrapper>{renderOptions()}</Wrapper>
    </Fieldset>
  );
};

export default RadioButtonGroup;

下面是单选按钮

const RadioButton = ({ label, id, disabled, ...rest }: InputElementProps) => {
  return (
    <Wrapper>
      <Radio id={id} type="radio" disabled={disabled} {...rest} />
      <Label htmlFor={id} disabled={disabled}>
        <span>{label}</span>
        {/* disabled && <DisabledIcon small /> */}
      </Label>
    </Wrapper>
  );
};

export default RadioButton;

也许在这个代码沙盒上更容易:https://codesandbox.io/s/custom-radio-button-group-forked-1do5u4?file=/src/App.tsx
知道什么是错误的吗?为什么当状态设置为useState<String>(drinks[1].label);时,默认情况下第二个选项没有选中?

gmol1639

gmol16391#

检查值似乎固定为defaultChecked={index === 0},也许可以替换为checked={label === selectedValue},使该组件完全受控。
派生生效日期:codesandbox
示例:
RadioButtonGroup中,包含selectedValue作为prop,以便子组件知道选择了哪个选项:

<RadioButtonGroup
  label="Select your drink:"
  selectedValue={selectedValue}
  options={drinks}
  onChange={radioGroupHandler}
/>

RadioButton中,使用checked代替defaultChecked进行条件检查:

<RadioButton
  value={label}
  label={label}
  key={optionId}
  id={optionId}
  name={name}
  disabled={disabled}
  // 👇 replaced: defaultChecked={index === 0}
  checked={label === selectedValue}
  onChange={onChange}
/>
ryevplcw

ryevplcw2#

https://codesandbox.io/s/custom-radio-button-group-forked-imbiw4?file=/src/components/inputs/RadioButtonGroup.tsx
您需要使其成为一组受控制的组件。现在,您只是使用本地HTML行为。传递您的值并手动设置选中哪个按钮

相关问题