reactjs - redux 表单字段的单选按钮
问题描述
它不能正常工作,我只想选择一个值,但我可以选择一个和多个......并且无法取消选择
该功能有代码。
const RadioButtonField = ({type, options, disabled, onChange}) => {
const handleChange = (value) => {
onChange(type, value);
};
return (
<div>
{options.map(({ value, label }) =>
<Radio.Group key={value}>
<Radio
disabled={disabled}
onChange={() => handleChange(value)}
>
{label}
</Radio>
</Radio.Group>
)}
</div>
);
};
解决方案
您可以参考以下代码:
class App extends React.Component {
state = {
initialValue: "A",
options: ["A", "B", "C", "D"]
};
onChange = e => {
console.log("radio checked", e.target.value);
this.setState({
value: e.target.value
});
};
render() {
return (
<Radio.Group value={this.state.initialValue}>
{this.state.options.map((value, index) => {
return (
<Radio onChange={this.onChange} key={index} value={value}>
{" "}
{value}{" "}
</Radio>
);
})}
</Radio.Group>
);
}
}
工作代码和框演示
我认为您不需要向该函数传递任何内容。每当单击该选项时,该event (e)
对象将被传递给onChange(e)
您,您将分别获得单击项的值和检查onChange(e)
e.target.value
值e.target.checked
。
推荐阅读
- javascript - 如何在Javascript中每24小时将变量重置为0?
- python - 在后台运行另一个python程序
- sql - SQL - 当没有大小写匹配时,什么情况下不应该检查结果?
- c++ - gst_parse_launch() 抛出“断言失败”
- php - PHP:关闭与客户端的连接而不缓冲
- powershell - putty中隧道设置的脚本
- go - Golang rpc 得到错误的结构变量
- python - 用 Python 抓取网页
- usb - 使用 Intel 的 xHC 启用插槽命令后,如何确定哪个 Root Hub 端口连接了 USB 设备?
- reactjs - 从 localstorage/Hooks 获取数据,然后删除它们