Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using Redux Sagas? [closed]

I'm still fairly new to Redux have not used Redux Sagas, so I'm unsure if this is a good situation to start using Sagas. I have a Redux action that calls a web API and, after getting the results back, may need to fetch data from an additional 1-20 APIs.

My first reaction is to just make a large Redux action with async-await, but it really feels like there is a better approach. Is Redux Saga what I need to research?

like image 464
ImmutableDonkey Avatar asked Apr 17 '18 14:04

ImmutableDonkey


People also ask

Is Redux saga still used?

Such a powerful & elegant tool as Redux-Saga, a Redux side effect manager, is said to be deprecated, and no longer being maintained, starting from Jan 27, 2021.

Should I use Redux saga?

While I wouldn't say Redux Saga is inherently better than any of the alternatives available, it does have a few benefits that might make you want to consider using it. Redux Saga offers a place completely decoupled from your action creators for you to handle your application's side effects.

What is the benefit of using Redux saga?

redux-saga is a library that aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, easy to test, and better at handling failures.

Is Redux saga difficult?

Unfortunately, for more than a few functions the code becomes harder to read, which makes it even more difficult to test.


1 Answers

The majority of projects use Redux Thunk to organize side effects. In principle, it gives a good result: it’s easy to read and test the code (but you still need to mock network requests). But it all is fair as far as the logic of your action creators (or thunks) is simple. Unfortunately, for more than a few functions the code becomes harder to read, which makes it even more difficult to test.

In this situation, Redux Saga comes to the rescue. Redux Saga is an alternative approach to the organization of side effects. Instead of dispatching functions processed by Redux Thunk you create saga and write all the logic of event stream processing. Unlike thunks that are carried out when you dispatch them, sagas run in the background right after the app is launched. Sagas observe all actions that the store dispatches and decide what to do with them.

At this point, we can allocate three key benefits of sagas:

  • Simplicity in organizing difficult side effects sequences;
  • Declarative style;
  • The simplicity of testing.

If you want to know how to put saga into practice, check our article: https://blog.s-pro.io/use-redux-saga/ where you'll find code examples and much more!

like image 50
S-PRO Avatar answered Oct 12 '22 00:10

S-PRO