我试图用slice()复制一个状态,然后操纵它并创建一个新状态
这是我的减速机:
case types.LOAD_AMOUNT_CHECKINS_SUCCESS: {
let buildings = state.buildings.slice();
buildings['devices_error'] = action.checkins['error];
.
.
.
return Object.assign({}, state,
{buildings: buildings}
);
}
但应用slice()会返回:[]
哪里有错误?
答案 0 :(得分:1)
要制作浅表副本,您只能在数组上调用slice() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
在您的代码示例中,您在不在数组上的对象上调用它。
答案 1 :(得分:1)
假设state.buildings
是一个对象(而不是数组),请将第二行更改为:
case types.LOAD_AMOUNT_CHECKINS_SUCCESS: {
let buildings = Object.assign({}, state.buildings);
buildings['devices_error'] = action.checkins['error];
.
.
.
return Object.assign({}, state,
{ buildings: buildings }
);
}