【发布时间】:2020-05-23 00:18:03
【问题描述】:
我正在查看结构化应用程序状态的文档化模式:
https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape
人们看到以下建议的方式来维护假设的博客应用程序的帖子:
posts : {
byId : {
"post1" : {
id : "post1",
author : "user1",
body : "......",
comments : ["comment1", "comment2"]
},
"post2" : {
id : "post2",
author : "user2",
body : "......",
comments : ["comment3", "comment4", "comment5"]
}
},
allIds : ["post1", "post2"]
}
我完全不清楚在该状态下维护allIds 字段有什么好处。下面的数据结构不是包含完全相同的信息吗?:
posts : {
"post1" : {
id : "post1",
author : "user1",
body : "......",
comments : ["comment1", "comment2"]
},
"post2" : {
id : "post2",
author : "user2",
body : "......",
comments : ["comment3", "comment4", "comment5"]
}
}
在我看来,第一种方法(即官方建议的方法)具有冗余信息,通常被视为缺陷。我看到的第一种方法的唯一好处是,如果我们使用byId 属性提前缓存一些帖子。我确信官方的 redux 文档有充分的理由建议这种模式。我在一些 React 应用程序中使用了 Redux,但没有太复杂,所以我肯定看不到什么。
【问题讨论】: