我最近才发现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,优点远大于缺点。我希望我的代码中少一些魔法。减少魔法的代价是更多的样板,但这是一个非常小的代价。

其他回答

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未来发展的方向(至于写这些行)。

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是通量的实现。

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

Redux作者在这里!

我想说的是,你将在使用它时做出以下妥协:

You'll need to learn to avoid mutations. Flux is unopinionated about mutating data, but Redux doesn't like mutations and many packages complementary to Redux assume you never mutate the state. You can enforce this with dev-only packages like redux-immutable-state-invariant, use Immutable.js, or trust yourself and your team to write non-mutative code, but it's something you need to be aware of, and this needs to be a conscious decision accepted by your team. You're going to have to carefully pick your packages. While Flux explicitly doesn't try to solve “nearby” problems such as undo/redo, persistence, or forms, Redux has extension points such as middleware and store enhancers, and it has spawned a young but rich ecosystem. This means most packages are new ideas and haven't received the critical mass of usage yet. You might depend on something that will be clearly a bad idea a few months later on, but it's hard to tell just yet. You won't have a nice Flow integration yet. Flux currently lets you do very impressive static type checks which Redux doesn't support yet. We'll get there, but it will take some time.

我认为第一个是初学者的最大障碍,第二个是过度热情的早期采用者的问题,第三个是我个人的烦恼。除此之外,我不认为使用Redux会带来Flux所避免的任何特定缺点,有些人甚至说它与Flux相比有一些优点。


请参阅我关于使用Redux的优点的回答。