简体   繁体   English

为什么纯还原剂在redux中如此重要?

[英]Why are pure reducers so important in redux?

Pure reducers have no side effects and enable things like time-travelling. 纯净的减压器没有副作用,可以实现时间旅行等。 They make reasoning about application behavior easier. 他们更容易推理应用程序行为。

This is intuitive to me. 这对我来说很直观。 But I cannot articulate WHY pure reducers lead to these positive non-functional attributes. 但我无法明确说明为什么纯粹的减速器会导致这些积极的非功能性属性。

Can someone help me articulate why making reducers side-effect free makes reasoning about application behavior easier? 有人能帮助我明确说明为什么减压器副作用免费使得应用程序行为的推理变得更容易吗?

Is it because you are guaranteed to have the exact same state after running the reducers? 是因为在运行Reducer之后你保证会有完全相同的状态吗?

If so, surely even side-effectful (ie. non-pure) reducers could have this property? 如果是这样,那么即使是有效的(即非纯粹的)减速器也可以拥有这种特性?

Is it because you are guaranteed to have the exact same state after running the reducers? 是因为在运行Reducer之后你保证会有完全相同的状态吗?

Yes, pure reducers are deterministic , meaning that if they are given the same input, they will always produce the same result output. 是的, 纯粹的减速器是确定性的 ,这意味着如果它们被赋予相同的输入,它们将始终产生相同的结果输出。 This property helps with situations like unit testing, because you know if a test passes once, it will always pass. 此属性有助于单元测试等情况,因为您知道测试是否通过一次,它将始终通过。

If so, surely even side-effectful (ie. non-pure) reducers could have this property? 如果是这样,那么即使是有效的(即非纯粹的)减速器也可以拥有这种特性?

No, impure reducers would rely on both the inputs and on the state of the application. 不,不纯的减速器会依赖于输入和应用程序的状态 They could behave a given way 1000 times while you're testing, but break when your application happens to be in a particular state that you never thought to test. 在测试时,它们可以以给定的方式运行1000次,但是当您的应用程序恰好处于您从未想过要测试的特定状态时,它会中断。

Of course, it's perfectly possible to have a gap in your unit testing that misses a corner case. 当然,完全可能在单元测试中存在间隙而错过了一个极端情况。 But if the test's results are 100% based on the inputs then you're far more likely to notice those corner cases just by looking at the stated inputs expected by the reducer. 但如果测试的结果是100%基于输入,那么你更有可能只是通过查看减速器预期的指定输入来注意那些极端情况。

If a function changes the application's state, then running the same function twice, or the same few functions in different orders, may cause completely different behavior. 如果函数更改了应用程序的状态,则运行相同的函数两次,或者以不同的顺序运行相同的几个函数,可能会导致完全不同的行为。 This makes it hard to reason about the correctness of the application because in order to know whether a given line of code is correct, you have to know what happened prior to calling it, possibly in a completely different part of the application. 这使得很难推断应用程序的正确性,因为为了知道给定的代码行是否正确,您必须知道在调用它之前发生了什么,可能在应用程序的完全不同的部分。

Is it because you are guaranteed to have the exact same state after running the reducers? 是因为在运行Reducer之后你保证会有完全相同的状态吗?

Yes, this is what makes pure-reducers the "gold standard". 是的,这就是使纯还原剂成为“黄金标准”的原因。 If the output depends only on the input, then it is very easy to test, replay, keep history, etc... 如果输出仅取决于输入,则很容易测试,重放,保存历史记录等...

If so, surely even side-effectful (ie. non-pure) reducers could have this property? 如果是这样,那么即使是有效的(即非纯粹的)减速器也可以拥有这种特性?

(not the popular answer). (不是流行的答案)。 This is also correct. 这也是正确的。 non-pure reducers could also have these same properties, if you are careful. 如果你小心的话,非纯粹的减速器也可以具有这些相同的属性。 However, it is much more error prone, and (conceptually) doesn't make much sense to do. 但是,它更容易出错,并且(概念上)没有多大意义。 The idea that (I think) you are getting at is that everything is just input and output. 我认为(我认为)的想法是,一切都只是输入和输出。 You could change the line of thinking a bit and consider the internal state of your "non-pure" reducer as one more input into your reducer. 您可以稍微改变思路,并将“非纯”减速器的内部状态视为减速器的另一个输入。

In that sense, you could imagine tracking your application state, your actions, and the internal state of your reducers, and end up with the same playback, etc, properties of your pure reducers (although you'd need a lot more code to handle that). 从这个意义上讲,您可以想象跟踪应用程序状态,操作以及Reducer的内部状态,并最终得到纯粹Reducer的相同回放等属性(尽管您需要更多代码来处理那)。

However, here's the rub: now you have your actual application state and your reducer's internal (and hidden) state. 但是,这就是问题:现在你有了实际的应用程序状态和reducer的内部(和隐藏)状态。 Who wants to keep track of two sets of state? 谁想跟踪两套国家? That is what really makes the testing, reasoning, and implementation more difficult. 这才真正使测试,推理和实施变得更加困难。 There are more "kinds" of things to keep track of, and it is easier to miss/forget key details. 有更多“种类”的东西要跟踪,更容易错过/忘记关键细节。 In essence, if you already have a large portion of your application dedicated to keeping track of state, why would you want to keep more state hidden in your reducers? 从本质上讲,如果您已经有很大一部分应用程序专门用于跟踪状态,那么为什么您希望将更多状态隐藏在Reducer中?

So even ignoring doing things right for the sake of "rightness", it is conceptually simpler for your overall system architecture to keep all of your state in just one place. 因此,即使为了“正确”而无视正确的事情,从整体系统架构的概念上来说,将所有状态保持在一个地方也是比较简单的。

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

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