【发布时间】:2019-09-05 19:45:27
【问题描述】:
我无法理解为什么会收到错误消息:
TypeError:无法分配给对象“#”的只读属性“描述”
我知道原则是我不想修改我的reducer中的状态。相反,我想返回一个新的状态副本。
这是我的减速器:
action TOGGLE_CHECKBOX:
{
let copyOfItems = [...state.items]; // create a new array of items
copyOfItems.forEach(i => i.description = "newDescription");
// return a new copy of the state
return {
...state,
items: copyOfItems
}
}
这是我的 Reducer 测试:
it ('Test that each item description is set', () => {
const state = {
items: [
{ description: "d1" },
{ description: "d2" }
]
}
deepFreeze(state);
expect(MyReducer(state, { type: TOGGLE_CHECKBOX })).toEqual({
items: [
{ description: "newDescription" },
{ description: "newDescription" }
]
});
});
但是,我收到上述错误消息。如果我删除 deepFreeze 测试通过。这意味着我以某种方式修改了原始状态,但我不知道为什么,尤其是因为我创建了一个新的展开项目数组。
任何帮助将不胜感激。
【问题讨论】:
标签: javascript reactjs redux