So I was going through the redux documentation on Reducers topic, and they had mentioned it should be pure functions. I understand this is for comparing whether the state object is the same or old. But I noticed the following code snippet at Redux Reducers documentation
case TOGGLE_TODO:
return Object.assign({}, state, {
todos: state.todos.map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: !todo.completed
})
}
return todo
})
})
Here they are directly accessing the state.todos array and using map operator. This means that map will update the state parameter passed. Considering reducer being pure function here, we should not update parameters itself here right?
Why are we/they not using slice() here like it should be below?
case TOGGLE_TODO:
return Object.assign({}, state, {
todos: state.todos.slice().map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: !todo.completed
})
}
return todo
})
})