If my state looks like this:
const initialState = {
color: 'blue',
sizes: ['S', 'M', 'L'],
}
And I want to change the color, my reducer should return this right:
return { ...state, color: newColor };
But won't the state still reference the old array? Is this considered a mutation even though I'm not modifying the array? Would this be better?
return {
...state,
color: newColor,
sizes: state.map.sizes(size => size)
}
And what if the array contains objects?
const initialState = {
color: 'blue',
sizes: [
{ color: 'red', size: 'S' },
{ color: 'green', size: 'M' },
{ color: 'blue', size: 'L' }
],
}
Does this mean I have to do this to avoid mutating the state?
return {
...state,
color: newColor,
sizes: state.map.sizes(item => { ...item })
}
Also, is there a reason Object.assign and the spread operator are preferred over doing JSON.stringify and JSON.parse?