samedi 29 février 2020

react input checkboxes - not toggling with change in state

I'm having trouble getting a group of checkboxes to toggle with changes in state. I'm rendering the checkboxes using array.map and returning input elements.

It looks like my handleUpdateCheck function updates state appropriately, but the DOM does not re-render.

const [checked, updateChecked] = useState([true, true, true, false, false]);

function handleUpdateChecked(index) {
    let newArr = checked;
    newArr[index] = !checked[index]
    updateChecked(newArr);
}

checked.map((box, index) => {
    return (
        <input
            key={index}
            type='checkbox'
            checked={checked[index]}
            onChange ={() => handleUpdateChecked(index)}
        />
    )
})


Thanks




Aucun commentaire:

Enregistrer un commentaire