Redux不变模式

Redux不变模式,redux,react-redux,immutability,Redux,React Redux,Immutability,我使用react with redux 行动: 请建议在嵌套数组中更新不可变属性的最佳方法: 减速器: 我的行动: action = { id: 1, bought: true } 我想在items数组中更新购买的属性。即: const updateClicked= (state, action) => { const updateSections = state.updates[action.section].items; const updatedItems

我使用react with redux

行动:

请建议在嵌套数组中更新不可变属性的最佳方法:

减速器:

我的行动:

action = {
  id: 1,
  bought: true
}
我想在items数组中更新购买的属性。即:

const updateClicked= (state, action) => {
    const updateSections = state.updates[action.section].items;
    const updatedItems = updateSections.map(el => {
        if (el.id === action.id && !el.bought) {
          el.bought = true;
        }

        return el;
    });

   //How to update state???
   return {}
};
如果您能解释两种方法,我们将非常高兴:

使用es6扩展运算符 使用一些类似于库的不变性助手 谢谢

使用es6扩展运营商:


我知道这不能回答你的问题。我将在稍后讨论:-但在讨论之前,我想建议您使您的商店状态正常化。因为如果你保持商店的状态正常化,那么写减缩器就会容易得多,它的性能会好得多,如果你使用记忆选择器,即重新选择你的应用程序,它的性能会好得多。长话短说:使用redux,尽可能保持商店的状态正常化。现在,我将继续回答你的问题…顺便说一句,那个动作的动作类型是什么?@Josep我已经更新了代码等待!我没有回答你问题的第二部分-
action = {
  id: 1,
  bought: true
}
const updateClicked= (state, action) => {
    const updateSections = state.updates[action.section].items;
    const updatedItems = updateSections.map(el => {
        if (el.id === action.id && !el.bought) {
          el.bought = true;
        }

        return el;
    });

   //How to update state???
   return {}
};
export default (state = initialState, action) => {
  if (action.type !== actionTypes.UPDATE_CLICKED) return state;
  return {
     ...state,
     updates: {
       ...state.updates,
       html: {
         ...state.updates.html,
         items: state.updates.html.items.map((item, idx) => idx === action.id
           ? {...item, bought: item.bought}
           : item
         )
       }
     }
  }
};