My app has a list of checkboxes along with a select/deselect all at the top. I store the currently selected checkboxes in a state array and when a checkbox is set a useEffect() is fired to drop some markers on a leaflet map. The select all will attempt to loop through the rows and set each checkbox to true which should subsequently fire the useEffect for each row.
At a high level the code is doing this:
const [checkboxStatus, setCheckboxStatus ] = useState([]);
const [currentRow, setCurrentrow] = useState(null);
useEffect(() => {
addMarkersToMap(currentRow)
}, [checkboxStatus]);
const handleCheckboxClick = (event, row) => {
setCurrentrow(row);
setCheckboxStatus(newSelected);
}
const handleSelectAllClick = (event) => {
props.rows.map((n) => {
setCurrentrow(n)
var chk = props.window.tablename + '_' + n.id + "_chk"
setCheckboxStatus(prevArray => [...prevArray, chk])
})
}
The single checkbox click works as expected - the checkboxStatus is updated and the useEffect fires to add my map markers for the current row. However the selectAll will only fire the useEffect for the final item in the list. I assume this has to do with some sort of asynchronous behavior and I probably am fundamentally doing it wrong :) Any insight is most appreciated.
Thanks
