gpt4 book ai didi

redux - 如何为 combineReducers 提供正确的状态切片

转载 作者:行者123 更新时间:2023-12-04 12:47:44 33 4
gpt4 key购买 nike

我是 Redux 的新手。

我一直在阅读它的'Reducers documentation , 遇到了我似乎无法理解的他们的 combineReducers 函数。

他们注意到:

All combineReducers() does is generate a function that calls your reducers with the slices of state selected according to their keys, and combining their results into a single object again. It's not magic.

我确实理解它会像听起来那样返回一个组合的 reducer,但是它如何为每个 reducer 提供特定的相关状态切片 - 根据它们的键什么意思?

深入了解他们的 It's not magic git issue 帮助我更多地了解了 combineReducers 的使用,但我仍然无法理解 according to their keys pars.

非常感谢任何解释,谢谢。

最佳答案

A:如果您的状态切片和 reducer 函数具有相似的名称 - 该切片将提供该 reducer。

示例:

const state = {
slice1 : {...},
slice2 : {...}
}

function slice1(state, action){...}
function slice2(state, action){...}

combineReducers({slice1, slice2}) //Reducers will be provided with appropriate slices.

B:如果您为 combineReducers 函数提供键,这些键必须与状态切片键匹配。

示例:

const state = {
slice1 : {...},
slice2 : {...}
}

function reducer1(state, action){...}
function reducer2(state, action){...}

combineReducers({slice1 : reducer1,
slice2 : reducer2}) //Reducers will be provided with appropriate slices.

C:两种方法可以混合使用:

示例:

const state = {
slice1 : {...},
slice2 : {...}
}

function slice1(state, action){...}
function reducer2(state, action){...}

combineReducers({slice1,
slice2 : reducer2}) //Reducers will be provided with appropriate slices.

关于redux - 如何为 combineReducers 提供正确的状态切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055532/

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