<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nisal-Sashmitha</title>
    <description>The latest articles on Forem by Nisal-Sashmitha (@nisalsashmitha).</description>
    <link>https://forem.com/nisalsashmitha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F916318%2F1e5ec67a-fad3-4206-ad0b-4a6db7c2a0ac.jpeg</url>
      <title>Forem: Nisal-Sashmitha</title>
      <link>https://forem.com/nisalsashmitha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nisalsashmitha"/>
    <language>en</language>
    <item>
      <title>Redux with React</title>
      <dc:creator>Nisal-Sashmitha</dc:creator>
      <pubDate>Wed, 07 Dec 2022 13:17:14 +0000</pubDate>
      <link>https://forem.com/nisalsashmitha/redux-with-react-1fn0</link>
      <guid>https://forem.com/nisalsashmitha/redux-with-react-1fn0</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyomm6mfinjka6yhsfhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyomm6mfinjka6yhsfhc.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;strong&gt;What is Redux?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply, this is a React management library for JavaScript Apps. If you don't know what state management is, It is managing state to keep data synced, shared, and updated across the application or necessary multiple data flows. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A little bit of background&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Redux was developed by Dan Abramov &amp;amp; Andrew Clark in 2015. With the inspiration of Flux. Flux is an application architecture that Facebook uses for building client-side web applications. It is more of a pattern to manage the state rather than a library or a framework. Redux is built up on that idea but in quite a different way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does it only work with react?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;, Redux can work with JavaScript libraries, frameworks, or anywhere even with vanilla JavaScript. Because it is just plain JavaScript. It can be run anywhere that JavaScript runs. Also, Specific bindings are available for most of the UI libraries and frameworks to work with Redux.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt;, Redux is mostly popular to work with React and specially made for working with React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Redux for React,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yeah, we have props and React Context already. But we know that when we use props in React, it gets messy. When the application grows, It decreases the readability, understandability, and even can be pushed to infinite loops by losing track.&lt;/p&gt;

&lt;p&gt;When it comes to React Context, it is ok to use React Context until your application is not a large-scale, enterprise-level application, or an application with high-frequency state changes.&lt;/p&gt;

&lt;p&gt;Here you can see that when an application has lots of things going through, either it will have to have lots of nested providers or one or two complex providers. So either way, it would be a mess.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnelotta5m8h63c36zst9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnelotta5m8h63c36zst9.png" alt="Image description" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We were talking about "Why Redux for React", So the direct reason is that Redux has solutions for all of those problems we discussed right now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Redux works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redux is all about having one data store for the entire application. It can be authentication states, user inputs or anything. All goes to this one store. It even sounds like a mess, right? No, we don't need to manage the entire store all the time, Therefore it won't be a mess. I will explain that within the upcoming few paragraphs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhc5rgezdrmp986q05ym0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhc5rgezdrmp986q05ym0.png" alt="Image description" width="800" height="791"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, the purpose of using this Redux in our React application is to track all the changes that we need, store them, and for reacting to those state changes in related components.&lt;/p&gt;

&lt;p&gt;For that, as you can see in the diagram, there is a central data storage, and components can set up a subscription to the central-storage. So, whenever the data changes, the storage notifies the subscribed components. And it can directly use the changed data. &lt;/p&gt;

&lt;p&gt;The good thing here is that you have ways to manage data or action loads using multiple reducers or data slices in different ways with different support libraries.&lt;/p&gt;

&lt;p&gt;So that is how we use data in the storage. Of course, we need to store state changes as well. However, components cannot directly manipulate the data in the storage. For that, as you can see in the diagram, we have a concept called reducer functions. This function is responsible for manipulating the data that is stored in our central store.&lt;/p&gt;

&lt;p&gt;But, how can we connect or use those reducers functions?&lt;/p&gt;

&lt;p&gt;For example, a login state change or a click of a button may need to change the central store's state. For that, you can see there is something called "action" in the diagram. We need to trigger this action from the component on a state change. If I use the word from Redux world instead of the word 'trigger'. It is, we need to &lt;strong&gt;dispatch&lt;/strong&gt; the action from a component.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;action&lt;/strong&gt; is a simple JavaScript operation that describe the kind of operation that needs to be performed on the dispatch call. Also, it includes the payload, which is the data that can be used in the reducer to perform changes. So, on the dispatch, the reducer function will run according to the action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Story of the diagram in Short&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If I conclude the diagram by reminding you the concept, There is a central storage. Components can have a subscription for the storage. And get changes from the storage. When data needs to be manipulated, we can dispatch an action from a component. Then the changes described in the action will be made by the reducer function. Then it will replace the existing state from the new state. That's the story of the Redux.&lt;/p&gt;

&lt;p&gt;One thing to keep in mind is that the reducer function must be pure, side effect free, and a synchronous function. You will understand it more in the next few paragraphs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side Effects, Async task &amp;amp; Redux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In previous paragraph I mentioned that reducer function should be pure, side effect free,and a synchronous function. So, how can we work in a situation that we have to use any of those on a state change?&lt;/p&gt;

&lt;p&gt;For example, you may have to update the database on a state change. One option is, you can get the current state from the store and manipulate it from the component and then save the changes in the store. Then we will not need some reducers. Because, all the manipulation part is happening in the component. If it is your personal preference and not harmful to the logic, then it is not a problem. But it is not the main idea behind using Redux.&lt;br&gt;
Another option is, you can write your data manipulation code in reducers and send your HTTP request or any of your side effect on that change in the component. That is also not an issue, you can do it as well.&lt;/p&gt;

&lt;p&gt;Or the best way is you can use a middleware. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use of middle ware in Redux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73exj3o8yauvui4vwr22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73exj3o8yauvui4vwr22.png" alt="Image description" width="796" height="750"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the suggested way to extend Redux with custom functionality. Basically this acts between the action and the reducer function. We can use this for logging, crash reporting, performing asynchronous tasks, etc. &lt;/p&gt;

</description>
      <category>web3</category>
      <category>ai</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>How to make a get request using axios with params and read it in backend.</title>
      <dc:creator>Nisal-Sashmitha</dc:creator>
      <pubDate>Sat, 03 Sep 2022 05:41:57 +0000</pubDate>
      <link>https://forem.com/nisalsashmitha/how-to-make-a-get-request-using-axios-with-params-and-read-it-in-backend-4g4</link>
      <guid>https://forem.com/nisalsashmitha/how-to-make-a-get-request-using-axios-with-params-and-read-it-in-backend-4g4</guid>
      <description>&lt;p&gt;Let's see how to make a get request using axios with params and read the request and get params in backend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FrontEnd&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//take 'docID' as the id of the ducement you search
//take port number as 4200

axios
      .get("http://localhost:4200/api/stock/getProduct", {
        params: { id: docID },
      })
      .then(function (response) {
        console.log(response.data);
      })
      .catch(function (error) {
        console.log(error);
      })
      .then(function () {
        // run anything after the response is arrived
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Backend&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const router = express.Router();

router.get("/getCusProducts", getProduct);

function getProduct(req, res) {
  let id = req.query.id; //&amp;lt;&amp;lt;&amp;lt;this is how you read params

  Product.findById(id)    // Product is the model
    .then((product) =&amp;gt; {
      res.json(product);
    })
    .catch((err) =&amp;gt; {
      console.log(err);
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>axios</category>
      <category>get</category>
      <category>mern</category>
      <category>axiosparams</category>
    </item>
  </channel>
</rss>
