It is always said that we should not mutate data in the reducer of Redux, or else there could be bugs. For example, we have to do:
case 'todos/todoToggled': {
return {
// Again copy the entire state object
...state,
// This time, we need to make a copy of the old todos array
todos: state.todos.map(todo => {
// If this isn't the todo item we're looking for, leave it alone
if (todo.id !== action.payload) {
return todo
}
// We've found the todo that has to change. Return a copy:
return {
...todo,
// Flip the completed flag
completed: !todo.completed
}
})
}
}
Suppose we don't care about the Undo / Redo capabilities, in which case we have to have all previous states, then on the official docs, it says:
- It causes bugs, such as the UI not updating properly to show the latest values
Why would UI not update properly? And is this the only bug? (that doc talk about other issue, such as harder to test... but those seem to be not bugs).
This is for deeper understanding of React / Redux.
I think I might have an answer but not entirely sure yet, but I will post 2, 3 days later and compare with other answers.