【发布时间】:2019-05-17 22:18:56
【问题描述】:
我的状态有这样的结构:
state
books {} <-- object
book <-- array
pages <-- array
在我的 reducer 中,我试图通过索引访问 book 数组并用新的 pages 数组替换它的 pages 数组。我在值更改之前和之后在我的谷歌浏览器中观察 Redux 值。 它将整个数组转换为一个对象。在 redux 中的 'book' 数组之前看起来像:
book: [{...}, {...}, {...}]
改动后:
book: {{0: {...}, 1: {...}, 2: {...}}
如何在 redux 中保持我的 book 对象的原始显示?
这是我的 reducer 中的代码:
export interface MyState {
book: BookItem[];
pages: Pages[];
}
function updatePages(index: number, state: MyState) {
// set up my new pages array here into newPages variable
return {
...state,
book: {
...state.book,
[index]: {
...state.book[index],
pages: newPages as Pages[]
}
}
};
}
【问题讨论】:
-
请发布更改前后的数据示例 - 特别是您希望数据在更改后的样子。目前尚不清楚您希望数据看起来如何。看看你的
reducer,很明显为什么你会得到这个结构{{0: {...}, 1: {...}, 2: {...}}
标签: reactjs typescript redux store