所以我有一个 React 代码,其中的复选框在我单击时不会切换。
这是codesandbox链接:https ://codesandbox.io/s/inspiring-kirch-q6p4h
我正在像这样初始化复选框状态:
const [checkbox, setCheckBox] = useState(() => {
let checkboxObj = {};
rows.forEach((e) => {
checkboxObj[e.id] = false;
});
return checkboxObj;
});
我们有一个列数组,其中有一个名为 customElement 的函数,其中包含复选框处理程序。
const columns = [
{
key: "Source_campname",
title: "TS Camp Name",
customElement: function (row) {
console.log(checkbox);
return (
<FormControlLabel
control={
<Checkbox
checked={checkbox[row.id]}
key={row.id}
onChange={() =>
handleChange(row.Source_campname, row.id, checkbox)
}
name={row.id}
/>
}
label={[row.Source_campname]}
/>
);
}
},
{
key: "Tracker_campname",
title: "TR Camp Name"
}
];
复选框切换在以下函数中处理:
const handleChange = (name, campid) => {
setCheckBox({ ...checkbox, [campid]: !checkbox[campid] });
};
发生的事情是当我点击它时复选框没有切换。知道发生了什么吗?