Awaiting A Reaction
by in CodeSOD on 2026-03-12Today's Anonymous submitter sends us some React code. We'll look at the code and then talk about the WTF:
// inside a function for updating checkboxes on a page
if (!e.target.checked) {
const removeIndex = await checkedlist.findIndex(
(sel) => sel.Id == selected.Id,
)
const removeRowIndex = await RowValue.findIndex(
(sel) => sel == Index,
)
// checkedlist and RowValue are both useState instances.... they should never be modified directly
await checkedlist.splice(removeIndex, 1)
await RowValue.splice(removeRowIndex, 1)
// so instead of doing above logic in the set state, they dont
setCheckedlist(checkedlist)
setRow(RowValue)
} else {
if (checkedlist.findIndex((sel) => sel.Id == selected.Id) == -1) {
await checkedlist.push(selected)
}
// same, instead of just doing a set state call, we do awaits and self updates
await RowValue.push(Index)
setCheckedlist(checkedlist)
setRow(RowValue)
}