#interestingProblem can anybody explain, please I had a problem while updating the state as in the first code block, but there was no problem when I updated the state as in the second code block as below.
I had a problem: (cannot assign to read-only property of object quantity)
const newItem = action.payload
newItem.quantity = 1
state.items = [...state.items, newItem]
I had no problem when I wrote the code like this
const newItem = action.payload
state.items = [...state.items, { ...newItem, quantity: 1 }]
the first approach you are mutating
action.payloaddirectly since you are not creating a copy tonewItembut passing the same reference. Givenaction.payloadis readonly you face the error:second approach works because you are creating a copy from
action.payloadnot mutating it:instead you should create a copy first to your approach to work: