我从我的 API 获取数据,并在将其转储到 redux 之前通过 normalizr 传递它。当我从 API 获取人员数据时,reducer 应该将它们附加到人员存储中。现在我的 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
}
}
第一个控制台日志是 reducer 使用一次后的状态。它有我保存到商店的最初一组人:
{
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 重复最后一个,并覆盖其他所有内容。