I have a table of data. The first column is a Checkbox. I'm trying to create an array of each row id that gets selected.
const [selectedRows, setSelectedRows] = useState([]);
const handleCheckbox = (event, row) => {
if (event.target.checked === true) {
selectedRows.push(row.id);
} else {
selectedRows.pop(row.id);
}
};
<TableBody>
{expenseTransactions.map((row) => (
<TableRow key={row.id}>
<TableCell>
<Checkbox onChange={(event) => handleCheckbox(event, row)} />
</TableCell>
</TableRow>
</TableBody>
I can't get it to work properly. Any ideas on the best way to handle the onChange event so I have an array of all selected elements, and then if i unselect one, that id would get removed from the array.
Thanks a tonne,