Redux文档建议使用normalizr这样设计状态的形状:
{
entities: {
cards: {
42: {
id: 42,
text: 'Hello',
category: 2
},
43: {
id: 53,
text: 'There?',
category: 1
},
}
categories: {
1: {
id: 1,
name: 'Questions'
}
2: {
id: 2,
name: 'Greetings'
},
}
},
filter: 'SHOW_ALL',
allCardsList: {
isFetching: false,
items: [ 42, 43 ]
},
}
Run Code Online (Sandbox Code Playgroud)
当然,这会分成三个可组合的缩减器(filter,allThingsList和entities),但在我看来,我想要为entities.cards和编写单独的缩减器entities.categories.
有没有办法将实体管理拆分为子集合器,允许这样的组合:
let rootReducer = combineReducers({
entities: {
things,
categories
},
filter,
allCardsList
});
Run Code Online (Sandbox Code Playgroud)
是否有任何优点保持cards和categories在entities,而不是保持在根级(这将允许使用组合物combineReducers)?
{
cards: { ... },
categories: { ... },
filter: 'SHOW_ALL',
allCardsList: { ... }
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将实体管理分成允许这样组合的子减少器?
当然!combineReducers()只给你一个减速器,你可以多次使用它:
let rootReducer = combineReducers({
entities: combineReducers({
things,
categories
}),
filter,
allCardsList
});
Run Code Online (Sandbox Code Playgroud)
Redux repo中的购物车示例演示了这种方法.
将卡和类别保留在实体中是否有任何优势,而不是保持根级别?
这取决于你,但我发现你建议的结构更容易使用.理解在单个密钥下对所有实体进行分组确实更容易,combineReducers()如上所示,这是可能的.
| 归档时间: |
|
| 查看次数: |
971 次 |
| 最近记录: |