{"id":10816,"date":"2022-03-22T11:42:57","date_gmt":"2022-03-22T11:42:57","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=10816"},"modified":"2024-06-25T11:03:34","modified_gmt":"2024-06-25T11:03:34","slug":"simplify-managing-application-state-with-javascript-library-react-redux","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/simplify-managing-application-state-with-javascript-library-react-redux","title":{"rendered":"Simplify Managing Application State with Javascript Library REACT-REDUX"},"content":{"rendered":"<table style=\"height: 276px;\" border=\"3\" width=\"360\">\n<tbody>\n<tr>\n<td>\n<h1><span style=\"color: #000080;\"><strong>TABLE OF CONTENT<\/strong><\/span><\/h1>\n<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#introduction\">1. Introduction<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#redux\">2. What is Redux<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#benefits\">3. Benefits of Redux<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#terminology\">4. Commonly Used Terminologies in Redux<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#flowchart\">5. Redux Flow Chart<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#getstarted\">6. Get Started With Redux<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#usecases\">7. Use Cases<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#conclusion\">8. Conclusion <\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#aboutcloudthat\">9. About CloudThat <\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#faqs\">10. Frequently Asked Questions (FAQs) <\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"introdution\"><strong>1. Introduction to Redux State Management:<\/strong><\/h2>\n<p>There are many popular libraries in JavaScript, and one of the most popular is React. React is based on a component-based approach which has made application development faster and easier. However, react is only the &#8220;View&#8221; part of the popular Model View Controller (MVC) framework. So, we need to keep track of the data while developing our application. Here, Redux comes into the picture and handles the application&#8217;s data flow from the backend of the application.<\/p>\n<p>So, in this blog, you will learn everything about integrating Redux with our React application.<\/p>\n<h3 id=\"redux\"><strong>2. What is Redux?<\/strong><\/h3>\n<p>Redux is an open-source library used to manage and change the state of our application globally as it creates a centralized &#8220;store&#8221; from which all the components in our application can access these states.<\/p>\n<h3 id=\"benefits\"><strong>3. Benefits of Redux?<\/strong><\/h3>\n<ol>\n<li>When using Redux, there is no need to keep states for every component. Instead, there will be a global state where we can store our data and be available to all the components in our application. In this way, it will be easier to maintain the application.<\/li>\n<li>Redux makes the state predictable.<\/li>\n<li>Debugging is easier.<\/li>\n<li>Redux has an extensive community that contributes to the betterment of the library.<\/li>\n<\/ol>\n<h3 id=\"terminology\"><strong>4. Commonly Used Terminologies in Redux<\/strong><\/h3>\n<ol>\n<li><strong>Store &#8211; <\/strong>A store is an object present centrally and keeps the record of various states in our applications and manages it using the dispatch function<strong>.<\/strong><\/li>\n<li><strong>Reducer <\/strong>\u2013 Reducer is a pure JavaScript function that takes the state of our application and action as arguments and then returns a new state. Here, we write the logic to update the state of our application.<\/li>\n<li><strong>Action <\/strong>\u2013 An action is a JavaScript object sent by the dispatch function to the reducer. It is the command to change the state. It has two properties&#8217; type&#8217; and &#8216;payload.&#8217; &#8216;type&#8217; is the action, and the payload is the value by which the user updates the application&#8217;s state.<\/li>\n<li><strong>Dispatch <\/strong>\u2013 Dispatch is the function that triggers a change in the state.<\/li>\n<\/ol>\n<h3 id=\"flowchart\"><strong>5. Redux Flow Chart<\/strong><\/h3>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10817\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux1.png\" alt=\"React Redux\" width=\"679\" height=\"436\" \/><\/a><\/p>\n<p>We will create a new project in ReactJS using redux to understand the flow better.<\/p>\n<h3 id=\"getstarted\"><strong>6. Get Started with Redux<\/strong><\/h3>\n<p>Open a code editor (I am using VS code) and open its terminal to create a new React project using the command<\/p>\n<pre class=\"nums:false nums-toggle:false lang:default decode:true\">npm install react-redux<\/pre>\n<p>After the project is created, create a component folder where all the components will be there. Next, create another folder, &#8220;Redux.&#8221; In this folder, we will place all our reducer files.<\/p>\n<p>To include redux in our application, open terminal and install react-redux<\/p>\n<pre class=\"nums:false nums-toggle:false lang:default decode:true \">npm install react-redux<\/pre>\n<p>In the Redux folder, create a new file &#8220;appleReducer.js&#8221; In this, we will create an initial state in our application with the property apple<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10819\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux2.png\" alt=\"React Redux\" width=\"445\" height=\"109\" \/><\/a><\/p>\n<p>Then we will create a reducer function which will take initialState and action as arguments<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10820\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux3.png\" alt=\"React Redux\" width=\"772\" height=\"99\" \/><\/a><\/p>\n<p>Based on the argument, the appleReducer function will perform the task on the state. For example, let&#8217;s say we want to increase the qty by 1, so our action type is &#8216;INC_QTY&#8217;, which will increase the qty or return the state.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10821\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux4.png\" alt=\"React Redux\" width=\"784\" height=\"379\" \/><\/a><\/p>\n<p>Lastly, we will export our appleReducer function. So, our reducer is now complete.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10822\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux5.png\" alt=\"React Redux\" width=\"409\" height=\"122\" \/><\/a><\/p>\n<p>Now we will create a global store. So, we will make a store.js file. In this file, we will import the createStore method from redux and appleReducer<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10823\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux6.png\" alt=\"React Redux\" width=\"717\" height=\"151\" \/><\/a><\/p>\n<p>Next, we will create a global store and export it<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10824\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux7.png\" alt=\"React Redux\" width=\"616\" height=\"179\" \/><\/a><\/p>\n<p>So, both our reducer and store are created.<\/p>\n<p>Now, we will create our apple component.<\/p>\n<p>We will create an &#8220;apple.js&#8221; file in our component folder. Here we will create a simple UI using a functional component with a heading and a button.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10825\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux8.png\" alt=\"React Redux\" width=\"603\" height=\"304\" \/><\/a><\/p>\n<p>We will create a mapStateToProps function that will return the state object as props to our component and a mapDispatchToProps function that will specify which action type to trigger.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10826\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux9.png\" alt=\"React Redux\" width=\"691\" height=\"397\" \/><\/a><\/p>\n<p>Lastly, we must connect and export our components with the store. This will be done with the help of connect function. So, we will first import the connect function.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10827\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux10.png\" alt=\"React Redux\" width=\"517\" height=\"91\" \/><\/a><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10828\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux11.png\" alt=\"React Redux\" width=\"903\" height=\"86\" \/><\/a><\/p>\n<p>Now import the Apple component in our App.js file. If we want to store all the components in our application, we must use the &#8216;Provider&#8217; component for it. So import it and wrap our component in it.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10829\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux12.png\" alt=\"React Redux\" width=\"613\" height=\"453\" \/><\/a><\/p>\n<p>In this way, the store will be available to the Apple component.<\/p>\n<p>Now the state will be available to our apple component as props. Our complete component code will look like this \u2013<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10830\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux13.png\" alt=\"React Redux\" width=\"903\" height=\"729\" \/><\/a><\/p>\n<p>Lastly, we will start our server to get the output in the browser. For that, start the terminal and run &#8220;npm start.&#8221;<\/p>\n<p>The output will look like this-<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10831\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux14.png\" alt=\"React Redux\" width=\"623\" height=\"290\" \/><\/a><\/p>\n<p>When we click Increase Quantity, the state will be updated, and the output will be printed accordingly.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10832\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/redux15.png\" alt=\"React Redux\" width=\"664\" height=\"271\" \/><\/a><\/p>\n<p>So, we can manage the state globally using Redux in this way.<\/p>\n<h3 id=\"usecases\"><strong>7. Use Cases:<\/strong><\/h3>\n<ol>\n<li>When our application is extensive and managing the states for each component becomes difficult.<\/li>\n<li>When the state of our application is frequently updated.<\/li>\n<\/ol>\n<h3 id=\"conclusion\"><strong>8. Conclusion<\/strong><\/h3>\n<p>In this blog, we have learned how to use Redux with React, its various features, and the proper use case to use Redux. It is not always necessary to use Redux in all our applications. Our application will still work without Redux.<\/p>\n<p>Feel free to ask any queries you may have while working on Redux, and I will be happy to help you.<\/p>\n<h3 id=\"aboutcloudthat\"><strong>9. About CloudThat<\/strong><\/h3>\n<p><a href=\"https:\/\/www.cloudthat.com\/\" target=\"_blank\" rel=\"noopener\"><strong>CloudThat\u00a0<\/strong><\/a>is\u00a0the official Microsoft Gold Partner, AWS Advanced Consulting Partner, and Training partner helping people develop knowledge on cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build\u00a0a robust\u00a0cloud computing ecosystem by disseminating\u00a0knowledge on technological intricacies within the cloud space.\u00a0Our blogs, webinars,\u00a0case studies, and white papers\u00a0enable all the stakeholders in the cloud computing sphere.<\/p>\n<p>Feel free to drop a comment or any queries that you have regarding React-Redux, cloud adoption, consulting and we will get back to you quickly. To get started, go through\u00a0our\u00a0<a href=\"https:\/\/www.cloudthat.com\/expert-advisory\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=expert-advisory\"><strong>Expert Advisory\u00a0<\/strong><\/a>page\u00a0and\u00a0<a href=\"https:\/\/www.cloudthat.com\/managed-services-packages\/\"><strong>Managed Services Package<\/strong><\/a><strong>\u00a0<\/strong>that is\u00a0<a href=\"https:\/\/cloudthat.com\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=cloudthat.com\/\"><strong>CloudThat<\/strong><\/a><strong>\u2019s<\/strong>\u00a0offerings.<\/p>\n<h3 id=\"faqs\"><strong>10. Frequently Asked Questions (FAQs)<\/strong><\/h3>\n<ol>\n<li><span style=\"text-decoration: underline;\"><strong>When should I learn Redux?<\/strong><\/span><\/li>\n<\/ol>\n<p>For a new learner, I will recommend that you first be comfortable with React and manage states using components. Then, as Redux is used to manage the condition, you will really understand the benefit of using Redux when your application grows.<\/p>\n<ol start=\"2\">\n<li><span style=\"text-decoration: underline;\"><strong>Can Redux be used with other frontend Frameworks like Angular and Vue?<\/strong><\/span><\/li>\n<\/ol>\n<p>Redux can also be used with React Native, Angular, Vue, and more because it is just used to store data for any UI layer.<\/p>\n","protected":false},"author":286,"featured_media":10933,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3607],"user_email":"satyamd@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/10816"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/286"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=10816"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/10816\/revisions"}],"predecessor-version":[{"id":46224,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/10816\/revisions\/46224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=10816"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=10816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}