我从我的API中获取数据,并在将其转储到redux之前通过normalizr传递。当我从API获取人员数据时,缩减程序应该将它们附加到人员存储中。现在,我的reducer正在覆盖存储中的所有现有数据。
减速机:
export default function reducer(state={
people: {
entities : {
people: {
0 : {
name : ''
}
}
},
result : [0,]
}
}, action) {
switch (action.type) {
case "GET_PEOPLE": {
const newpPeople = {...state.people, ...action.payload };
console.log(state.people)
console.log(action.payload)
console.log(newpPeople)
return {...state, people : newpPeople};
}
default :
return state
}
}
第一个控制台日志是减速器使用一次后的状态。它有我保存到商店的初始人员集:
{
entities: {
people : {
1 : { id: 1, name: "jim" },
2 : { id: 2, name: "billy" }
}
},
result : [ 1, 2 ]
}
第二个控制台日志将是要添加的新人的有效负载:
{
entities: {
people : {
7 : { id: 7, name: "sally" },
8 : { id: 8, name: "ana" }
}
},
result : [ 7, 8 ]
}
那么第三个控制台日志应该是这两个状态的组合吗?但它只是重复了sally和ana的上一个,并覆盖了其他所有内容。
https://stackoverflow.com/questions/41379580
复制相似问题