我最近才发现Redux。一切看起来都很好。使用Redux比Flux有什么缺点,缺点或妥协吗?谢谢


当前回答

Both Redux and Flux require a considerable amount of boilerplate code to cover many common patterns, especially those that involve asynchronous data fetching. The Redux documentation already has a handful of examples for boilerplate reduction: http://redux.js.org/docs/recipes/ReducingBoilerplate.html. You could get everything you might need from a Flux library like Alt or Fluxxor, but Redux prefers freedom over features. This could be a downside for some developers because Redux makes certain assumptions about your state that could be inadvertently disregarded.

你真正回答你的问题的唯一方法是尝试Redux,如果你可以的话,也许在一个个人项目中。Redux的出现是因为开发人员需要更好的体验,它倾向于函数式编程。如果你不熟悉函数概念,比如约简器和函数组合,那么你可能会慢下来,但只是稍微慢一点。在数据流中采用这些想法的好处是更容易测试和可预测性。

免责声明:我从Flummox(一种流行的Flux实现)迁移到Redux,优点远大于缺点。我希望我的代码中少一些魔法。减少魔法的代价是更多的样板,但这是一个非常小的代价。

其他回答

Redux要求关于不变性的纪律。我可以推荐ng-freeze,让您了解任何意外的状态突变。

Both Redux and Flux require a considerable amount of boilerplate code to cover many common patterns, especially those that involve asynchronous data fetching. The Redux documentation already has a handful of examples for boilerplate reduction: http://redux.js.org/docs/recipes/ReducingBoilerplate.html. You could get everything you might need from a Flux library like Alt or Fluxxor, but Redux prefers freedom over features. This could be a downside for some developers because Redux makes certain assumptions about your state that could be inadvertently disregarded.

你真正回答你的问题的唯一方法是尝试Redux,如果你可以的话,也许在一个个人项目中。Redux的出现是因为开发人员需要更好的体验,它倾向于函数式编程。如果你不熟悉函数概念,比如约简器和函数组合,那么你可能会慢下来,但只是稍微慢一点。在数据流中采用这些想法的好处是更容易测试和可预测性。

免责声明:我从Flummox(一种流行的Flux实现)迁移到Redux,优点远大于缺点。我希望我的代码中少一些魔法。减少魔法的代价是更多的样板,但这是一个非常小的代价。

据我所知,redux的灵感来自于通量。flux是一个类似MVC(模型视图控制器)的架构。facebook在使用MVC时由于可扩展性问题引入了流量。所以通量不是一个实现,它只是一个概念。实际上redux是通量的实现。

Flux和Redux…

Redux is not a pure Flux implementation but definitely inspired by Flux. Biggest difference is that it uses a single store that wraps a state object containing all the state for your application. Instead of creating stores like you'll do in Flux, you'll write reducer functions that will change a single object state. This object represent all the state in your app. In Redux you will get the current action and state, and return a new state. That mean that actions are sequential and state is immutable. That bring me to the most obvious con in Redux (in my opinion).

Redux支持一个不可变的概念。

为什么不变性?

原因如下: 1. 相干存储的状态总是由减速器改变,因此很容易跟踪谁改变了什么。 2. 性能——因为它是不可变的,Redux只需要检查之前的状态!==当前状态,如果是,则呈现。不需要每次循环状态以确定呈现。 3.调试-新的很棒的概念,如时间旅行调试和热重新加载。

更新:如果这还不够说服你,看看Lee Byron关于不可变用户界面的精彩演讲。

Redux需要开发人员通过代码库/库来维护这一思想。您需要确保以不可变的方式选择库和编写代码。

如果您想了解更多关于Flux概念的不同实现(以及最适合您的需求的实现),请查看这个有用的比较。

说了这么多,我必须承认Redux是JS未来发展的方向(至于写这些行)。

相对于其他Flux替代品,使用Redux的最大好处之一是它能够将您的思想重新定位为更实用的方法。一旦你理解了这些线是如何连接起来的,你就会意识到它在设计上令人惊叹的优雅和简单,而且永远不会回头。