I've a value in my state to store a list of cities, a checkbox to select them in a table, and an event listener to add the selected value to the state.
I can toggle the checkbox one time, from false to true. Then, it can't toggle to false. I'm logging the state and it's correctly updated, so I don't understand why my checkbox isn't updated.
For example :
- The "New York" check box isn't checked, so state is []
- I can check it, the state changes to ["New York"]
- When I uncheck it, the state changes to [], but nothing happens in the checkbox
Here's the code :
const [selected, setSelected] = useState([]);
<input type="checkbox" onChange={(e) => handleRowSelect(e, bpf.city_name)} checked={selected.indexOf(bpf.city_name) > -1} />
const handleRowSelect = (e, city) => {
        let arr = selected;
        const index = arr.indexOf(city)
        if (index === -1) {
            arr.push(city);
        } else {
            arr.splice(index, 1);
        }
        setSelected(arr);
        console.log(arr)
        console.log(selected)
        dispatch({ type: 'SET_SEL_BPF', payload: selected })
    }
 
     
    