I wanted to get a feel for react hooks and was trying them out by making a normal todo list with adding and removing capabilities but I have come across an annoying behaviour and I do not know the logic behind it.
const handleDeleteTodos = (event) => {
todos.splice(event.target.name, 1);
setTodos(todos)
window.localStorage.setItem('Todos', JSON.stringify(todos));
};
So the code above does delete the element, I know this because when I reload the page, the element is gone. But it doesn't re-render like the add todo function. While the code below works.
const handleDeleteTodos = (event) => {
const newTodos = [...todos];
newTodos.splice(event.target.name, 1);
setTodos(newTodos)
window.localStorage.setItem('Todos', JSON.stringify(newTodos));
};
I only difference I see is the object destructuring aspect of the code.
I just wanted to know what is the logic behind this behaviour.