State Management · Junior · Question 5 of 75
How do you remove an item with id from an array in state?
- ✓Correct answer:
setItems(items.filter(i => i.id !== id)) - 2
items.splice(index, 1); setItems(items) - 3
delete items[index]; setItems(items)
Explanation
State must be updated immutably: filter, map, spread and toSorted create a new array. splice and delete mutate the existing one, the reference stays the same, and React skips the update.