Managing UI state within complex web applications and mobile applications of present times is difficult for all developers/Engineers. Every dynamic web and mobile app needs to use asynchronous tasks. In this blog, we will discuss how we manage app UI states using Redux and what will be the most suitable and easiest middleware along with Redux for a project. We will also compare Redux-Saga vs. Redux-Thunk.
Redux is a state management library and can be used with any library or framework. It is an open-source, cross-platform library for managing the application states. Redux is widely and easily used in many libraries, React, React-native, and others. The primary use of Redux is that we can use a single application state as a global state, and interacting with the state of the application from any component is very easy if they are parent or child.
npm install redux redux-logger react-redux –-save
Redux follows a unidirectional data flow. 3 major components are in redux structure: ‘actions’, ‘reducers’, and the ‘store’. Other components are ‘components(UI)’, ‘Middlewares’, ‘Redux Thunk’.
Actions: Actions are JavaScript objects that contain payloads of information that send data from the application to the redux store. These are the only information sources for the redux store. That means if any state change is necessary for any reason, the change required will be dispatched through the actions. It has a ‘type’ property that specifies the type of the action we are performing and can also have a ‘payload’ property used to send some data to redux stores.
Reducers: Reducers are functions that determine the changes in the application state and return the updated state. Actions describe something happening inside but don’t specify how the application’s state changes in response. When an action is dispatched for state change, the reducer has to make the necessary changes to the state and return the new state of the application. They take action as the argument and update the state inside the redux store.
Store: A redux store can be created With the help of reducers which holds the entire state of the application. It is recommended to use a single store for the entire application rather than having multiple stores, which will violate the use of redux, which only has a single store.
Components (UI): This is the section where the UI of the application is kept.
Middlewares: Redux middleware is a function that sits between action and reducer and can contact the dispatched action before reaching the reducer function. Popular middleware libraries which allow for side effects and asynchronous activities, there are Redux-Thunk and Redux-Saga.
The middleware lets you call the action creators that return a function instead of the action object after receiving the redux store’s dispatch method, which is used to dispatch the regular synchronous actions inside a function’s body after the asynchronous operations have been completed.
The thunk is a concept of idea in programming where a function is used to delay the evaluation and calculation of an operation inside an app.
npm install redux-thunk –-save
Redux-thunk Store object example
Example of Redux Thunk
Redux Saga library is another middleware that helps with API calls or side effects. It is the most popular competitor for Redux Thunk. Redux saga uses an ES6 feature called ‘Generators’, which helps write asynchronous code.
Generators are the functions that came with ECMAScript6 of javascript. Surprisingly, the generator functions can be paused, resumed, exited between the execution, and re-entered later during the operations. The name of the generator function name can be changed as per requirement. I have used the fetchDashboardSaga( ) function.
The benefit of Redux Saga compared to Redux Thunk is that you can more easily test your asynchronous data flow inside an application.
npm install redux-saga –-save
Example of Store object of redux-saga
Example of Action
A Generator function is that function that does not execute it at once; instead, it returns an iterator object. The function body is executed when calling the method on an iterator object until the “yield” keyword is inside the function.
The next() method returns an object with a property called “value,” which has some value, and the yield property has returned it. A “done” property stands for whether a function execution is completed or not.
Redux Saga listens that the actions dispatched and triggers an API call or any other side effect of your written code.
Below is an example of how we can call an API endpoint using Redux Saga.
Example of a redux-saga
By calling the put function, we can trigger actions just like we did with dispatch. It allows adjusting and handling of our reducer to handle our actions.
Saga works like a separate thread or by a background process, So for that is only responsible for making side effects inside APIs or API calls, unlike redux-thunk, in some cases, which uses callbacks that lead to situations like ‘callback hell.’ Every time with the async/await system, this problem can be minimized in redux-thunk.
Example of reducer object
Thunks can’t ever act in response to an action. On the other hand, Redux-Saga subscribes to the redux store and will trigger a saga to run, pause or continue when an action is dispatched.
Redux Thunk | Redux Saga |
Action creators may hold too many async logic functions | Action creators stay pure functions |
Contains less boilerplate codes | Contains more boilerplate codes than Redux-Thunk |
Difficult to scale up codes | Easy to scale codes as compared to redux-thunk codes |
Difficult to test async functions | Easy to test as all async logic remains together |
As compared to redux-saga, easy to understand logic, functions, concepts | Due to multiple concepts to learn like generator functions and redux-saga effects, etc. It is difficult to understand. |
In this blog, we saw in detail the use of Redux, Redux-Thunk, and Redux-Saga, and the difference between Redux Saga vs. Redux Thunk.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork