Before going to the topic “Redux Workflow Features” we should look at the question “What is Redux?”
What is Redux?
“A Predictable State Container for JS Apps”. In other words, Redux is a pattern-based library that is used to manage and update the application state. For managing and updating the application state Redux used events known as “action”.
Redux has some properties which make it popular some of them are described below:
- Redux has the predictable property means it can run in different environments like the client, server, and native.
- Redux Centralized the application state from which you can easily perform actions like undo, redo, state persistence, and many more.
- Redux also has the property of debugging, in other words, Redux has Redux Devtools which make applications easily debuggable like when, where, why, and how your application state is changed.
- Also Redux has the property of flexibility means it can work on any type of UI layer.
Let’s take an example to understand redux easily. Let’s imagine an application like a food app where we can manage the recipe using the Redux Store. For managing the recipe in the application we have some basic methods which help us to do so like:
- We have a single store to manage the whole state of the application.
- Also, actions method to make necessary changes we want to do.
- reducers which are like safety guards that know how to mutate state based on the requested action.
- And at least middleware is used to handle the task performed in the application.
Redux Workflow:
In Redux workflow is unidirectional. This means it can flow singly only not going to reverse order is possible. Also, it maintains the flow of data in a very efficient manner. As the application has complex programming and works then managing the state also becomes complex until we have management or control over the state of the application. So, for this reason, we have to maintain the state of the application. This can happen in the following steps which are known as the redux workflow.
Steps to workflow in Redux:
- When the user interacts with the application and performs some actions. Then these actions are dispatched by the dispatcher. This means that when the user do some changes in the application then these actions sent a request to the store. For changing the state of the application.
- After receiving the request for the action at the store. The store is called the root reducer function to perform an action and provide the next stage of the application. The root reducers perform the task using the divide-conquer. The root reducer divides the task in the smaller reducer function which all together performs the whole change state of the application.
- Then these changes are sent to the middleware to test the actions whether these actions are valid for the store or not. And also perform the necessary changes in the action and then send them to the store.
- When a store gets information about changing the state of the application. Store notifies the changes to the registered listeners that a state change has occurred. Then various parts of the application will be updated as required.
So, from these steps, the whole application is updated, reset, modify, and many more operations.
Redux Workflow Features: There are some important features that are used to maintain the workflow of the redux. Which are described below:
1. Actions and Actions Creators:
As we know that if we have an application that stores something and performs some actions which seem to be changeable we have to make it necessary in the state of the application. For this, we have to perform some actions and this is the only way in Redux. Redux is nothing more than plain JavaScript objects which are holding the information needed for changing the state of the application.
Example of an action object
{ type: 'INCREMENT', payload: { Id: 'main', amount: -100 } }
A function that creates an action object
function increment(Id, amount) { return { type: 'INCREMENT', payload: { ID, amount } }; };
2. Reducers:
If an action is sent to the store, then the store needs to change the state according to the action. For this, it calls known Reducers, It takes the current state of the application and returns the action that needs to be performed on the state.
In other words, we can say that reducers are function that takes two parameters like current state and action. And return to the next state. Syntax of the function:
function callNextState(state, action) { ... return nextState; }
Note- Reducers never modify the state; they always create a copy of the needed changes in the state.
3. Middleware:
The middleware is a test feature of Redux. Because it tests the actions before they reach to store. Middleware can change the actions, create more actions, etc. Middleware uses the dispatch() function. It is a more powerful tool in Redux.
4. Store:
Redux has a single store for the whole application. But the store doesn’t have any user logic. So, when the actions are encountered at the store. Store send them to the middleware. Middleware tests the actions and makes necessary changes and then sent them to the reducers. Reducers take state action and return to the next state. After all this store receives a that causes a change to the state. The store notifies all the listeners that a change to the state has occurred. Then the various parts will be updated as the application have to modify.
From all these things the workflow happened.