Dan Feinstein Dan Feinstein - 8 months ago 45
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


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 .