简体   繁体   English

Redux-状态如何传递给减速器?

[英]Redux - how is state passed to reducers?

I'm just wrapping my head around Redux and I just wanted to run a question by on how state is passed to reducers: 我只是把头放在Redux上,我只是想问一个关于state如何传递给reducer的问题:

I understand that the structure of state is dictated by the combineReducers() function and that the keys that you define within this function will become the various properties of state . 我知道state的结构是由combineReducers()函数决定的,并且您在此函数中定义的键将成为state的各种属性。

Am I correct in saying that only the subset of state that is associated with a particular reducer is passed to the reducer? 我是否正确地说, 只有与特定减速器关联的state子集传递给减速器? Or is the entire state object passed? 还是整个state对象都通过了?

Having lots of 'ahahhh' moments with Redux...! Redux有很多“ ahahhh”时刻...!

combineReducers takes a number of reducers as parameters and returns a reducer. combineReducers将大量的reducer作为参数,并返回一个reducer。 The particular reducer created by combineReducers only passes the part of the state assigned to a particular reducer. combineReducers创建的特定Reducer仅传递分配给特定Reducer的状态部分。 This way each reducer is completely in charge of its own part of the state and nothing more. 这样,每个reducer完全负责自己的状态部分,仅此而已。

But you can write your own kind of combineReducers that could work differently. 但是您可以编写自己的类型的CombineReducers,它们的工作方式可能有所不同。 This is just to say that there is nothing special about combineReducers . 这只是说combineReducers没什么特别的。

A reducer is just a function that takes a state and an action and returns the state with the action applied in some way. 减速器只是一个函数,它接受一个状态和一个动作,并以某种方式应用该动作返回状态。 Or not, at its discretion. 是否自行决定。

For instance, in Redux it is common for separate branches of the state to handle the same (set of) actions but handle them in different ways that make sense for that part of the state. 例如,在Redux中,状态的各个分支通常处理相同的(一组)动作,但是以对状态的那部分有意义的不同方式来处理它们。 Each action is passed to all sub-reducers by combineReducers , giving each branch's reducers a chance to either do something, or nothing. 每个动作都由combineReducers传递给所有子combineReducers ,从而使每个分支的combineReducers器有机会做某事,或者什么都不做。

So in most common usage, a reducer is solely responsible for a slice of the state and sub-reducers, such as those passed to combineReducers, or reducers called by your own reducers, are solely responsible for their sub-state/branch. 因此,在最常见的用法中,还原器仅对状态的一部分负责,而子还原器(例如传递给CombineReducer的那些子还原器,或由您自己的还原器调用的还原器)对子状态/分支负责。

But your own reducers may call a sub-reducer and pass it anything you like, such as the same state that was already passed to some other reducer, or even some data it created without it ever having been available in the global state. 但是您自己的化简器可能会调用子化简器并将其传递给您喜欢的任何东西,例如已经传递给其他化简器的相同状态,甚至是它创建的某些数据,而该数据在全局状态中始终不可用。

These things are not common, but if you see a need, and feel confident to go forward with it, you are encouraged to use what you believe works best. 这些事情并不常见,但是如果您看到需要,并且有信心继续前进,则鼓励您使用自己认为最有效的方法。 Great power comes with great responsibility. 强大的力量伴随着巨大的责任。

combineReducers is just a helper function for a common use case. combineReducers只是一个常见用例的辅助功能。 It's probably cleanest to make a single reducer responsible for a particular branch of the state, but there may be situations where you find it beneficial to do things differently. 使单个reducer负责状态的特定分支可能是最干净的方法,但是在某些情况下,您发现以不同的方式做事是有益的。 A single reducer can manage a complex object with nesting without calling any other reducer, but a common pattern is to make smaller reducers to handle parts, if the parent is getting unwieldy. 单个化简器可以通过嵌套管理复杂的对象而无需调用任何其他化简器,但是一种常见的模式是,如果父级变得笨拙,则使较小的化简器可以处理零件。

The main point is to realize that a reducer is always a pure function that takes state and an action and returns a state. 要点是要认识到,约化器始终是一个接受状态和操作并返回状态的纯函数。 How you make this happen is entirely up to you. 如何做到这一点完全取决于您。 Most developers choose to follow common patterns seen in other's code, such as Dan's Redux examples or the videos he has on egghead.io. 大多数开发人员选择遵循他人代码中常见的模式,例如Dan的Redux示例或他在egghead.io上的视频。

Treading on the beaten path is often a good way to start out because it has already been vetted. 在人迹罕至的地方踩踏通常是一个很好的起点,因为它已经过审查。

By the same token, however, it is always important to know why something is done. 但是,出于同样的原因,了解为什么要做某事总是很重要的。 Developers sometimes make things more complex than it needs to be because of following an example that either was meant for something else or wasn't thought through very well to begin with. 开发人员有时会使事情变得比原本复杂的多,这是因为遵循了一个示例,该示例要么用于其他目的,要么一开始就没有被很好地考虑过。

Also read this article by Dan. 也请阅读 Dan的这篇文章。

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

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