- Posted by webuters
- On December 27, 2019
- 0 Comments
Introduction to Redux Saga
What is Redux Saga? How does it execute different functions?
This post will shed some light on what it is all about. In addition, it will outline how Redux Saga works.
What is Redux Saga?
Technically, Redux-Saga can be defined as a library that helps overcome the side effect in connection with Redux.
Redux Saga is a Redux Middleware. As such, it obstructs the actions of Redux and replaces the functionality of the latter with its own.
The word “saga” refers to the effect produced by a code. This can be anything ranging from a procedure that remains in the cache to action such as an HTTP request. A collection of sagas constitutes a middleware. As far as the number is concerned, it can range from a few to several sagas. The middleware works in tandem with Redux store.
The state of Redux-based applications changes as soon as one starts an action. As a result, an intervention becomes necessary to restore the default state of coding at the start of every fresh cycle. Whether you wish to retrieve data from a server or send an event, this is important for all actions.
To keep things simple, one can consider saga as a distinct thread in an application. The former shares a cause and effects relationship with the latter. Redux Saga can start, pause or cancel a saga by performing common redux actions in the main application.
Redux saga has access to the whole application state. It carries out all functions based on this feature. It works in the background which enables it to perform all its tasks without a fuss. Apart from highlighting its usefulness, it also constitutes one of the major reasons for its popularity.
How Redux Saga works?
The first step of saga functioning begins with the operation and execution of a promise. After this cycle, the middleware suspends the saga until the time a promise is resolved.
After the latter step, the middleware restores the saga until the determination of the yield statement. Thereafter, the saga is suspended again until the resolution of the promise. This circle continues unabated.
Redux saga package involves numerous functions, some of which are intended to achieve specific objectives. These are called special functions. By using these functions, the saga code generates effects.
On average, a Redux Saga package involves the following helpers:
Following the initiation of an effect, a saga remains stalled until its fulfillment.
If at any point in time, one feels the need to run effects simultaneously or in parallel, one can do so by applying the following two commands:
The command CANCEL_TASK is applied in the end to stop a task from going ahead.
The aforementioned helpers are applied to codes to execute the functions of Redux Saga. The whole process is repeated from the beginning till the end at the start of each cycle.
Redux Saga complements the functionalities of Redux by allowing users to begin a saga afresh. Generating fresh codes in general after the execution of a promise may not be a big deal for a developer in normal situations. But if done constantly, it can prove to be a monotonous and uphill task. Thus, the introduction to Redux Saga becomes a necessity.