Dan Feinstein Dan Feinstein - 9 months ago 51
AngularJS Question

Redux: Does this count as mutating state?

Using Redux within Angular2 (NgRedux) for updating/setting an order entry. Is it ok to mutate a proxy variable, then object.assign it to state?

In my data reducer:

case DataActions.UPDATE_ORDER:
var updatedItem
var updatedState = state.orders
for(var i = 0; i < state.orders.length; ++i) {
if(state.orders[i]['key'] === action.payload.key) {
updatedItem = state.orders[i]
updatedItem.name = action.payload.name
updatedItem.items = action.payload.items
updatedState[i] = updatedItem
return Object.assign({}, state, {
orders: updatedState

Answer Source

Your "proxy variable" is just another reference to the same object, so yes, you are directly mutating the items in that array.

In order to correctly do immutable updates, each level of nesting should be copied. The Redux docs give examples of both common mistakes, and how to do this correctly, in http://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html .