为什么纯还原剂在redux中如此重要? [英] Why are pure reducers so important in redux?

查看:114
本文介绍了为什么纯还原剂在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?

如果是这样,那么即使是副作用(即非纯的)还原剂也肯定可以具有此特性?

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?

是的,归约器是确定性的,这意味着如果给它们相同的输入,它们将始终产生相同的结果输出.此属性可用于单元测试之类的情况,因为您知道一次测试通过一次,它将始终通过.

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?

不,不纯归约的还原剂将同时依赖于应用程序的输入状态.在您进行测试时,它们可能以给定的方式运行1000次,但是在您的应用程序碰巧处于您从未想到要测试的特定状态时,它们就会崩溃.

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.

当然,在单元测试中完全有可能遗漏一些极端情况.但是,如果基于输入的测试结果是100%,那么仅通过查看reducer预期的输入,您就更有可能注意到这些极端情况.

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.

如果某个功能更改了应用程序的状态,则两次运行相同的功能,或者以不同的顺序运行相同的几个功能,可能会导致完全不同的行为.这使得很难推理应用程序的正确性,因为为了知道给定的代码行是否正确,您必须知道在调用它之前发生了什么事情,可能是在应用程序的完全不同的部分.

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.

这篇关于为什么纯还原剂在redux中如此重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆