The ‘checked’ value on my list is not changing, i don’t know why that is
I’m making a toDo app
My State Values:
const [newItem, setNewItem] = useState("");
const [toDos, setToDos] = useState([]);
My function
function toggleToDo(id, completed) {
setToDos((currentTodo) => {
return currentTodo.map((todo) => {
if (todo.id === id) {
return { ...todo, completed};
}
return todo;
});
});
}
My List
<ul className="list">
{toDos.map((toDo) => {
const { id, title, completed } = toDo;
return (
<li key={id}>
<label htmlFor="">
<input
type="checkbox"
checked={completed}
onChange={(e) => toggleToDo(id, e.target.checked)}
/>
{title}
</label>
<button className="btn btn-danger">Delete</button>
</li>
);
})}
</ul>
i am expecting the ‘e.target.checked’ to change when the function toggleToDo is called
2
Answers
My label
<label htmlFor="">
was not linked thus my functionality was not workingThe
checked
attribute does not indicate whether this checkbox is currently checked: if the checkbox’s state is changed, this content attribute does not reflect the change.The
value
attribute is the one that changes when the checkbox state changes.(see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox#additional_attributes)