Which redux middleware should I choose?

2019-05-22 12:30发布

Following the guide, I came to a sorts of middlewares are there for redux application.

Redux Thunk, Redux Promise, Redux Promise Middleware, Redux Observable, Redux Saga, Redux Pack

It is just a matter of preference around which middleware we choose. But I'm not asking for choosing a convention.

I would like to know if there are any differences between them like performance, browser support, use cases, etc. Or anything else that I'm missing with. I tried a hard research for these but not able to find any article.

So that I can consider choosing a middleware for my application. It will also be great for me to choose middleware if I know the particular use case with different middlewares.

Or, all middlewares are just conventions and I may choose any one of them for any kind of redux application (small or large)?

2条回答
叼着烟拽天下
2楼-- · 2019-05-22 13:02

To be able to choose one of these libraries we must take into account whether we are building a small or large application. Usability, code standards, and JavaScript knowledge may also be considered. All of them are similar.

redux-thunk​

Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. It incorporates the methods dispatch and getState as parameters.

​redux-saga​

redux-saga is a library that aims to make application side effects (i.e. asynchronous like data fetching and impure procedures such as accessing the browser cache) in a manageable and efficient way to execute. It's simple to test as it uses the ES6 feature called generators, making the flow easy to read as synchronous code.

redux-observable

redux-observable is a middleware for redux that is inspired by redux-thunk. It allows developers to dispatch a function that returns an Observable, Promise or iterable of action(s). When the observable emits an action, or the promise resolves an action, or the iterable gives an action out, that action is then dispatched as usual.

And others directly from their github source:

redux-promise

The middleware returns a promise to the caller so that it can wait for the operation to finish before continuing. This is especially useful for server-side rendering.

redux-promise-middleware

Redux promise middleware enables robust handling of async action creators in Redux: it accepts a promise and dispatches pending, fulfilled and rejected actions. The middleware can also be combined with Redux Thunk to chain action creators.

redux-pack

redux-pack is a library that introduces promise-based middleware that allows async actions based on the lifecycle of a promise to be declarative.

Async actions in redux are often done using redux-thunk or other middlewares. The problem with this approach is that it makes it too easy to use dispatch sequentially, and dispatch multiple "actions" as the result of the same interaction/event, where they probably should have just been a single action dispatch.

This can be problematic because we are treating several dispatches as all part of a single transaction, but in reality, each dispatch causes a separate rerender of the entire component tree, where we not only pay a huge performance penalty, but also risk the redux store being in an inconsistent state.

redux-pack helps prevent us from making these mistakes, as it doesn't give us the power of a dispatch function, but allows us to do all of the things we were doing before.

查看更多
Fickle 薄情
3楼-- · 2019-05-22 13:17

From my personal experience ( I have used most of the middleware you have listed ).

Redux Saga is the way to go. It has a higher learning curve, but once you wrap your head around it, it becomes extremely powerful

Redux Saga

  1. Allows for separation of concerns
  2. Reduced Side Effects
  3. Cancelable tasks ( amazing )
  4. Parallel(like) processing

Once you understand redux saga, it becomes much easier to extend your application.

https://engineering.universe.com/what-is-redux-saga-c1252fc2f4d1

查看更多
登录 后发表回答