I have been exploring redux-saga lately and thought to put down a simple counter example here using Redux saga with TypeScript to manage the counter state across the application.
We will start with with a simple create-react-app with a Counter Component. In the first part of the article, we will see how to modify the state of the counter without using redux. And in the second part, we’ll maintain the state using redux saga. I hope this helps some of you too.

The application is built using the template flag, as Typescript contains only one component which we display on the app. The component styles will be named dynamically as per the name we define for the style in our module.scss.d.ts file.
In the counter component,
We have two simple buttons to increment and decrement the counter value. This counter value is maintained in the count variable and we either increase or decrease the count value using the useState hook.
As per reactjs.org:
Hook is a special function that lets you “hook into” React features. For example, useState is a Hook that lets you add React state to function components.
The initial value of count is set to zero as here:
- const [count, setCount] = useState(0);
Now using the setCount method, we can set the state as what ever is specified without actually mutating the state.

Simple, right?
Now this is a small component with just one value to be taken care of, however as our components increase, we might want to have a store where we literally store all the important data. For example, what if the state of the counter value is taken care of globally, and not at the component level?
This is when we come to using Redux, a state management library for managing state throughout the application.
In this part, we’ll see how to use Redux saga to perform counter increment, decrement and an asynchronous operation of incrementing the counter value. This is because we go for middleware like Thunk, redux-saga when dealing with async operations.
Using redux-saga for counter async
Now, without having a count value in the counter component, we will keep this in a global state and create sagas to deal with it.
Redux saga is a companion library for redux that helps to manage the async flow of our app. Think of redux saga like a thread that is responsible for taking care of all the side effects that might generate when performing async operations. And these can be handled using the same redux operations.
Let us look at a regular function.
- function anonymous() {
- return ;
- }
This returns only one value. What if you want to return multiple values?
Redux saga uses generators to return multiple operations from a function. Also what if we want to pause the execution of a function in between, we cannot do that unless we return something.
So a generator can be used to enhance the power of functions. Let us create a generator function.
- function* anGenerator() {
- return 'one';
- }const value = anGenerator();
- console.log(value) // --> {[Iterator]}
As you see here, this not only returns ‘hi’ but a generator object/iterator. This helps us to iterate over the multiple values returned by the geneerator.
We use the keyword yield to do that.
- function* anGenerator() {
- yield 'one'
- return 'two';
- }const value = anGenerator();
- console.log(value.next()); // {value: one, done: false}
- console.log(value.next()); // {value: two, done: true}
- console.log(value.next()); // {value: undefined, done: true}
As you see, the generator gets paused after one usage of next method, and finally when the values are complete, the done gets true indicating that it is complete and the value now becomes undefined.


Saravanakumar SekaranPosted Aug 1, 2021, 6:03 AM
Very Nice Article !!!