简体   繁体   English

combineReducers 如何知道要通过 REDUX 的切片

[英]How combineReducers know which slice to pass REDUX

I'm little bit confused about how combineReducers will pass reducers their needed slice of state.我对 combineReducers 如何将 reducer 传递给他们所需的状态片有点困惑。

For example i my state looks like this:例如,我的状态如下所示:

{
    products: [somedata],
    comments: [somedata]
}

and I have 2 reducers for handling changes in them (1-productReducer, 2-commentReducer)我有 2 个减速器来处理其中的变化(1-productReducer,2-commentReducer)

in my root reducer i have在我的根减速器中,我有

const rootReducer = combineReducer(
    products: productReducer,
    comments: commentReducer
)

Lets imagine products and comment are two different things and have no connection together and i want to just pass state.products to productReducer and state.comment to commentReducer.让我们想象一下产品和评论是两个不同的东西,并且没有联系在一起,我只想将state.products传递给 productReducer 并将state.comment给 commentReducer。 That so obvious I don't need comments in productReducer .如此明显,我不需要productReducer注释。 But how does this combineReducer handle this?!但是这个combineReducer如何处理这个?!

If I want to do this I would write:如果我想这样做,我会写:

function rootReducer(state, action) {
    products: productReducer(state.products),
    comments: commentReducer(state.comments)
}

But I wonder combineReducer even cares about slicing data and passing them to appropriate reducer.但我想知道combineReducer甚至关心切片数据并将它们传递给适当的减速器。

I see people are linking to the source code or the documentation but maybe a simple explanation will help you understand.我看到人们链接到源代码或文档,但也许一个简单的解释会帮助你理解。

When you call combine reducers, each reducer will return it's initial slice of the pie, combine reducers simply takes this slice of the pie and stores it in a map using the same name as to which the reducer is registered, when an action is dispatched, it simply takes the slice it was stored for each reducer and passes it again in order to produce the next state.当你调用 combine reducers 时,每个 reducer 将返回它的初始饼图,combined reducer 只是简单地获取饼图的这个切片,并使用与注册 reducer 相同的名称将它存储在 map 中,当一个 action 被分派时,它只是获取它为每个减速器存储的切片并再次传递它以生成下一个状态。

you call:你打电话:

combineReducers({
  foo: fooReducer,
  bar: barReducer,
})

internally the state gets stored:状态在内部被存储:

combinedState = {
  foo: fooState, // the state returned by the initial call to the foo reducer
  bar: barState, // the state returned by the initial call to the bar reducer 
}

when an action is dispatched, something like this occurs:当一个动作被分派时,会发生这样的事情:

combinedState = {
  foo: fooReducer(fooState, action), // generate the next state
  bar: barReducer(barState, action), // generate the next state
}

combineReducer(products: productReducer, comments: commentsReducer) , will create single state object and differentiate between them using keys (products, comments) passed to it. combineReducer(products: productReducer, comments: commentsReducer)将创建单个状态对象并使用传递给它的键(产品、评论)来区分它们。

rootReducer = combineReducer(products: productReducer, comments: 
commentsReducer);
   // This would produce the following state object
  {
  products: {
   // ... products, and other state managed by the productReducer ...
  },
  comments: {
   // ... comments, and other state managed by the commentsReducer
  }
}

Here this concept is defined.这里定义了这个概念。

Update:更新:

combineReducer calls all reducers it's wrapping with action dispatched, giving reducers a chance to respond. combineReducer调用它包装的所有减速器,并发送动作,让减速器有机会做出响应。 Please follow .关注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM