I am learning react hooks and I am just trying to do a simple function to remove the item from the list. To do this I am using find, indexOf, and splice.
In the onClick function, I am using the indexOf in the array.splice(indexOf, 1), BUT it returns just the item from the list. Everything rerenders and does what it is supposed to do, but the only itme that renders is the item that I just tried to delete. What am I missing?
const [todos, setToDo] = useState(initialToDo);
const [input, setInput] = useState('');
const todoList = (todos) => {
return (
todos.map((todo) => {
return (
<div className='todo-list flex p-2 w-1/2 justify-between'>
<p className="px-3">{todo.name}</p>
<button
className='rounded-sm border-2 px-2'
onClick={(e)=>{
let item = todos.find(el=>el.id == todo.id);
console.log(item)
let index = todos.indexOf(item);
console.log(index)
todos = todos.splice(index, 1)
// todos == item
setToDo(todos)
}}
>-</button>
</div>
)}))
}