【发布时间】:2020-05-14 13:33:41
【问题描述】:
我的 Redux 状态有问题。我制作了一个应该更新数组中的值的减速器。但是,我相信我遵循了所有不变性原则,但由于某种原因,我的状态没有更新。我什至在 reducer 的末尾添加了一个控制台日志,比较旧的 en 新状态,它返回 false。
我错过了什么吗?
我正在使用 Redux DevTools chrome 插件来检查状态。
状态:
selectedProfileId: mainProfileId,
profiles: {
[mainProfileId]:{
name: "Spellbook 1",
classes: [],
learnedSpells: {
cantrip: [],
first: [],
second: [],
third: [],
fourth: [],
fifth: [],
sixth: [],
seventh: [],
eighth: [],
ninth: []
},
spellSlots:[
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0},
{slots: 0, used: 0}
]
}
}
减速机:
switch (action.type) {
case actions.SET_SPELL_SLOTS:
let newSpellSlotArray = state.profiles[state.selectedProfileId].spellSlots.slice();
newSpellSlotArray.map((item, index) => {
if (index !== action.spellslot-1) {
return item
}
let newItem = {
...item,
slots: parseInt(action.count)
};
console.log("Updating item:", item, newItem);
return newItem;
})
var newState = {
...state,
profiles: {
...state.profiles,
[state.selectedProfileId]:{
...state.profiles[state.selectedProfileId],
spellSlots: newSpellSlotArray
}
}
}
console.log((newState == state))
return newState
default:
return state
}
控制台日志:
Updating item: {slots: 0, used: 0}
slots: 0
used: 0
__proto__: Object
{slots: 5, used: 0}
slots: 5
used: 0
__proto__: Object
spellbookData.js:70
false
【问题讨论】:
标签: reactjs redux react-redux