gpt4 book ai didi

redux - 你可以使用 combineReducers 来组合深度状态 reducer 吗?

转载 作者:行者123 更新时间:2023-12-04 23:17:35 25 4
gpt4 key购买 nike

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 ]
},
}

自然,这会分成三个可组合的 reducer ( filterallThingsListentities ),但在我看来,我想为 entities.cards 编写单独的 reducer 和 entities.categories .

有没有办法将实体管理拆分为允许组合的子 reducer :
let rootReducer = combineReducers({
entities: {
things,
categories
},
filter,
allCardsList
});

保留 cards 有什么好处吗?和 categoriesentities ,而不是保持在根级别(这将允许使用 combineReducers 进行组合)?
{
cards: { ... },
categories: { ... },
filter: 'SHOW_ALL',
allCardsList: { ... }
}

最佳答案

Is there a way to split management of entities into subreducers that would allow composition like this?



当然! combineReducers()只是给你一个 reducer ,所以你可以多次使用它:
let rootReducer = combineReducers({
entities: combineReducers({
things,
categories
}),
filter,
allCardsList
});

shopping-cart example in Redux repo演示了这种方法。

Are there any advantages to keeping the cards and categories in entities, instead of keeping on the root level?



这取决于你,但我发现你建议的结构更容易使用。将所有实体分组在一个键下确实更容易理解,并且可以使用 combineReducers()正如我上面展示的那样。

关于redux - 你可以使用 combineReducers 来组合深度状态 reducer 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com