Getting Started With React Redux : Part1

Leave a Comment
We know that nowadays JavaScript is trending like never before, the market for single page applications (spa) is demanding a lot of changes in terms of performance, reusability and many other features. When we work with the large-scale application, at a time the main aspect of the application is to manage app cache and state throughout the application, the cycle of updating to the models will be tough to manage.
Specifically, in react, we have worked with state and if you not aware so you need to learn about state first, and if you are aware of React than you may hear about state and stateful components. So let me tell you that Redux is the thing which will help us to remove direct DOM rendering and manage states at a single place.

What is Redux

As per the official Redux docs, a state can be described into the three different principles.

Single Source of Truth

The state is the main focus in Redux, I mean to say that in your whole application, there will be a single object tree who manages the state for all the pages of our React application. The changes are made into the application will be directly reflected in the state or you can say a single object tree of the state which located in a separate location.

State is Read-only

The state is always read-only, it means we cannot directly write nor do modification in states, but we can maintain by using actions which the most comprehensive part of redux as well. By emitting the action, we can change the state value and further we can proceed with the state data.

Changes are made with pure functions

We know already that we can change state via action so for that we can write pure reducers. Reducers are the pure functions which can take different actions data and also contains previous state data and at last, it will return updated data. Please find the following simple example of reducer.
In this example, you can see that I have created different action types for the Authentication module.
  1. const auth = (state = initialSettings, action) => {
  2. switch (action.type) {
  3. case SIGNIN_USER:
  4. return {
  5. ...state,
  6. signin: action.payload
  7. };
  8. case AUTH_USER:
  9. return {
  10. ...state,
  11. authenticated: true
  12. };
  13. case UNAUTH_USER:
  14. return {
  15. ...state,
  16. authenticated: false
  17. };
  18. case AUTH_ERROR:
  19. return {
  20. ...state,
  21. errorInfo: action.payload
  22. };
  24. return {
  25. ...state,
  26. accountDetails: action.payload
  27. };
  28. case RESET_SUCCESS:
  29. return {
  30. ...state,
  31. resetSuccess: action.payload
  32. };
  33. default:
  34. return state;
  35. }
  36. };
  38. export default auth;
You can see into above example that, every time when any action will be triggered, at the same time it returns previous state data along with updated data and will return updated and fresh data related to authentication operations like Auth success, Auth error, Auth user, Unauth use and Account details.

How Redux Works..

How Redux Works
This is the simple diagram I have created to show the flow that how redux work actually, starting from the action to be a trigger to the end part where the store will have updated state and then again this cycle will be followed based on multiple operations.Do not worry about the flow for now because we are going to learn everything in details late on this article.

Redux core concepts

In this introductory part of Redux tutorial, we will go through four different but very essential part of Redux which are listed below.
  • Reducer
  • Actions
  • Store


Reducer is a kind of pure function, and it returns a combination of previous state data and newly updated data, at last, it will return the updated state to the store.Reducers data will always depend on actions result and response. In our React with Redux application, we can have different reducer based on the module, like we can have different reducers for the Auth module, Employee module, and Common function module. For example, we have a module for employee management and for CRUD operation we have sample module like this.
  1. function reducer(state, action) {
  2. switch (action.type) {
  3. case 'ADD':
  4. return { ...state, added: true }
  5. case 'DELETE':
  6. return { ...state, deleted: false }
  7. case 'UPDATE':
  8. return { ...state, ...action.data }
  9. default:
  10. return state;
  11. }
  12. }
In this example, we have different action types like Add, Delete and Update, so based on the action dispatched, our state will be updated their values.


Whenever our store will be configured, our next step is to dig into Actions, do not confuse with action, actions are the function which occurs different operation like Add data, Update data, Select data and Delete data using the API.
Let me give you a simple example of Reset password function which is going to reset the user’s password.
  1. export function resetSuccess(message) {
  2. return {
  3. type: RESET_SUCCESS,
  4. payload: message
  5. };
  6. }
In above example, our action type is RESET_SUCCESS and we are going to send the message about the action which we have performed as payload to the reducer. One thing about action to keep in mind that it always consist type key which is used to represent every action uniquely.


Ahh, we left the discussion about Store, now it's show time because the value of the store in Redux is high because without redux there is no use of Reducers and Actions.In one sentence :
  • A store is an object which combined the Reducers and ActionsYes, it’s true; Store is building-block for Redux and used to combine all the reducer and actions which are created into your application.
  • Because I told you before in this article that there will be a single combined store in your application, and state of the whole application will be maintained inside the single store.
For that, we can create a combined reducer and then we will pass it to the store.
  1. const reducers = combineReducers({
  2. settings: Settings,
  3. auth: Auth,
  4. createproposal: CreateProposal,
  5. dashboard: Dashboard,
  6. commonfunctions: CommonFunctions
  7. });
  9. export default reducers;
Here in the above code snippet, we have different reducers like auth, settings, and dashboard and common functions. So we are going to combine them all in a single piece and going to pass to the store just like below lines of code.
  1. import reducers from './reducers';
  2. const store = createStore(reducers);
As you can see that I have imported our combined reducer, and created a store with the combined reducer.

Why Redux

Yes, it’s an obvious question you may have that why I should learn and uses redux, what are the needs to learn react. When we are working with large scale applications, sometimes it may happen that we confused about that how to manage everything in a proper manner with ease, same time react takes all responsibility to manage everything about your application changes and states.The main advantage is that :
Learn Redux (once) Use it in = Angular, React and Vue.js (Trust me, we can) We know that React is JavaScript based library and we don’t have any restrictions like Angular which is a Framework (not judging), and yes its true that it is one of the famous libraries to work with.

Advantages of Redux

We have covered so many different things about Redux so far, let me list down few primary advantages about Redux.
  • Single application state
  • Minimal code
  • Small functionality yet effective results
  • Hot reloading
  • Easy to learn, Easy to write
  • Immutable data structure
And I want to add one more thing that community support is growing like never before for React and Redux because more and more peoples are engaged with this library and they have started using React into their business applications.So these are the primary advantages what I have felt so far about redux, and there are many more when you try to work with redux.
In this introductory part of Redux, we have covered so many different aspects of the Redux which are listed below for the recap.
  • Redux introduction
  • What is Redux
  • Core concepts of Redux
  • Why we should Redux
  • Advantages to using Redux
    Thus it depends on our project requirement to choose like when we can choose Redux with React, not every time we should use it, but I can say it has so many advantages over other libraries or frameworks, Carry on learning with Redux, Thanks for reading.


Post a Comment

Note: only a member of this blog may post a comment.