So I have a React code in which the checkbox is not toggling when I click on it.
This is the codesandbox link: https://codesandbox.io/s/inspiring-kirch-q6p4h
I am initializing the checkbox state like this:
const [checkbox, setCheckBox] = useState(() => {
let checkboxObj = {};
rows.forEach((e) => {
checkboxObj[e.id] = false;
});
return checkboxObj;
});
We have a column array in which there is a function called customElement which contains the checkbox handler.
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"
}
];
Checkbox toggle is handled in the function below:
const handleChange = (name, campid) => {
setCheckBox({ ...checkbox, [campid]: !checkbox[campid] });
};
What is happening is that the checkbox is not toggling when I click on it. Any idea what is going on?