Why use Redux-Observable over Redux-Saga?

前端 未结 7 1204
太阳男子
太阳男子 2020-12-22 15:22

I have used Redux-Saga. Code written with it is easy to reason so far, except JS generator function is messing up my head from time to time. From my understanding, Redux-Obs

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 15:52

    Since there's a whole bunch of redux-observable talk in here, I'd thought I'd give the saga side of the argument. I don't use redux-observable or RxJS, so I can't give a side by side comparison, but I have used sagas to great effect.

    For what its worth, I'm using sagas in production in a web application.

    Sagas vs. Thunk

    Saga wins hands down. I didn't like how thunk put logic in my action creators. It also made doing a few requests in a row troublesome. I briefly looked at redux-observable for this job, but settled on Sagas.

    Learning Curve for Sagas

    Understanding what generators are and why they're important is key to understanding sagas. But I will stress that you don't need to know generators inside and out. You only need to know that you're passing off control with the yield statement, and that the saga will pass back control after your async code resolves. After that bit, its not very hard to understand what's going on in a saga.

    The core saga methods are (in my experience):

    • call - Call any bit of code and get the return value. Supports promises. Great synergy between async processing and sagas.
    • select - Call a selector. This bit is rather brilliant. Selectors are core to redux, and they are 100% supported!
    • put - aka dispatch an action. In fact dispatch as many as you want!

    There are other functions, but if you can master those three, you will be in a really good spot.

    Conclusion

    The reason I chose sagas was the ease of use. redux-observable looked like a challenge. I am 100% satisfied with sagas. More happy than I ever expected.

    In my experience, Sagas are (way) better than thunks and relatively easy to understand. Rx is not everyone's cup of tea. I would strongly consider sagas instead of redux-observable if you don't come from that ecosystem and/or don't plan on using Rx in the future.

提交回复
热议问题