<?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: Hardeep Singh</title>
    <description>The latest articles on Forem by Hardeep Singh (@hardeepbhandal).</description>
    <link>https://forem.com/hardeepbhandal</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%2F403279%2Ff8b12da9-1966-4479-a606-ecc0562c120b.jpg</url>
      <title>Forem: Hardeep Singh</title>
      <link>https://forem.com/hardeepbhandal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hardeepbhandal"/>
    <language>en</language>
    <item>
      <title>Practical use of array.reduce() in JavaScript</title>
      <dc:creator>Hardeep Singh</dc:creator>
      <pubDate>Wed, 01 Sep 2021 18:03:10 +0000</pubDate>
      <link>https://forem.com/hardeepbhandal/practical-use-of-array-reduce-dee</link>
      <guid>https://forem.com/hardeepbhandal/practical-use-of-array-reduce-dee</guid>
      <description>&lt;p&gt;&lt;a href="https://media.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%2F8qgtmos0cf2ry60z5ruv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8qgtmos0cf2ry60z5ruv.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;The reduce() method executes a reducer function for each value of an array. reduce() returns a single value which is the function's accumulated result. &lt;/p&gt;

&lt;h3&gt;
  
  
  How reduce works:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sumofAllNumbers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sumofAllNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;reduce()&lt;/em&gt; has two parameters&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;reducer/iterator function  e.g function(sum,number)&lt;/li&gt;
&lt;li&gt;Initial value  0 in this case  (optional)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;reduce() method will iterate over each number and add it to sum variable as shown in example. &lt;br&gt;
*initial value=0&lt;br&gt;
*iteration 1 : sum=10&lt;br&gt;
*iteration 2 : sum=30&lt;br&gt;
*iteration 3 : sum=60&lt;br&gt;
*return 60 to sumofAllNumbers variable&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Example
&lt;/h3&gt;

&lt;h4&gt;
  
  
  To Balance Parenthesis
&lt;/h4&gt;

&lt;h5&gt;
  
  
  For example : Following are valid parenthesis
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;()()()&lt;/li&gt;
&lt;li&gt;((()))&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Invalid parenthesis
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;(()))&lt;/li&gt;
&lt;li&gt;)()
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;balanceParenthesis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Check input is valid string &lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;// special case when we have ) at start&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt;
                    &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//counter as initial value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//if functions return 0 then Parenthesis are balanced&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;balanceParenthesis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;()()()&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parenthesis are balanced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parenthesis are not balanced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we converted provided input to array using string.split("")&lt;br&gt;
Then we applied reduce function to iterate over each character &lt;/p&gt;

&lt;p&gt;In reduce method we checked if character is '(' then we increased counter value by 1 &lt;br&gt;
other wise decreased the value by -1 for ')'&lt;/p&gt;

&lt;p&gt;Special check &lt;em&gt;if(counter&amp;lt; 0)&lt;/em&gt; added to check cases like  ")(" as we will have counter value 0 in this case &lt;/p&gt;

&lt;p&gt;So in result if the counter value is 0 then parenthesis are balanced otherwise these are not. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Building Cryptocurrency  Chart from Scratch in React - Part 1  </title>
      <dc:creator>Hardeep Singh</dc:creator>
      <pubDate>Mon, 24 May 2021 19:01:56 +0000</pubDate>
      <link>https://forem.com/hardeepbhandal/building-cryptocurrency-chart-from-scratch-in-react-part-1-2i4n</link>
      <guid>https://forem.com/hardeepbhandal/building-cryptocurrency-chart-from-scratch-in-react-part-1-2i4n</guid>
      <description>&lt;p&gt;Let's start with the basic app is the react to display Cryptocurrency of the day.&lt;/p&gt;

&lt;p&gt;In this blog, you will learn following &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to create a react app from scratch &lt;/li&gt;
&lt;li&gt;How to use useState Hook&lt;/li&gt;
&lt;li&gt;How to use useEffect Hook &lt;/li&gt;
&lt;li&gt;Calling an API using Axios&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creating App in react&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create React App is a comfortable environment for learning React and is the best way to start building a new single-page application in React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app crypto-tracker
cd crypto-tracker
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(To execute the above commands Node.JS must be installed on your system you can use the following link to install Node.JS&lt;br&gt;
&lt;a href="https://nodejs.org/en/download/"&gt;https://nodejs.org/en/download/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;crypto-tracker&lt;/strong&gt; is app name. &lt;/p&gt;

&lt;p&gt;You can open crypto-tracker in the Visual Studio Code so that you can start developing your react app.&lt;/p&gt;

&lt;p&gt;You will see the following structure of your application&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zz7HatiW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4esychz28x244mmuq09.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zz7HatiW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4esychz28x244mmuq09.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see app.js file &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DreOHtTH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1npedv9wds3trvcc8qc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DreOHtTH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1npedv9wds3trvcc8qc.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing Axios&lt;/strong&gt; (Promise based HTTP client for the browser and node.js)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using Axios&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To use axios first we need to import axios&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will get CryptoCurrency for the day data from REST API.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Let's create API folder in the src folder and add CryptoAPI.js *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UirC8eyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ombixyruqnfxruv9v7l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UirC8eyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ombixyruqnfxruv9v7l.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add following code in CryptoAPI.js&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CryptoAPI.js&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;import React, { Component } from 'react';
import axios from 'axios';

export default class CryptoAPI extends React.Component {
  static getCoinOfTheDay() {
    return new Promise((resolve, reject) =&amp;gt; {
      const url =
        'https://api.lunarcrush.com/v2?data=coinoftheday&amp;amp;key=XXXXXXXXXXXXXXXXX';
      axios
        .get(`${url}`)
        .then(res =&amp;gt; {
          resolve(res.data.data);
        })
        .catch(err =&amp;gt; reject(err));
    });
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have successfully implemented code to call API using Axios.&lt;/p&gt;

&lt;p&gt;It's time to create a component that will display the Currency Name and its symbol. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Presentation folder in the src folder and add CoinOfTheDay.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d3zQi09x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l80x9jg8s09d3obqkpou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d3zQi09x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l80x9jg8s09d3obqkpou.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CoinOfTheDay.js&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;import React from 'react';

function CoinOfTheDay(props) {
  return (
    &amp;lt;div&amp;gt;
      {props.data &amp;amp;&amp;amp; (
        &amp;lt;div&amp;gt;
          &amp;lt;span&amp;gt; Coin of the Day&amp;lt;/span&amp;gt; &amp;lt;br /&amp;gt;
          &amp;lt;span&amp;gt; Name : {props.data.name} &amp;lt;/span&amp;gt; &amp;lt;br /&amp;gt;
          &amp;lt;span&amp;gt; Symbol : {props.data.symbol} &amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}

export default CoinOfTheDay;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will pass data to CoinOfTheDay.js to display. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding CoinOfTheDay in the App.js and Passing data from API call&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;import { useState, useEffect } from 'react';
import './App.css';
import CryptoAPI from './Api/CryptoAPI'; 
import CoinOfTheDay from './Presentation/CoinOfTheDay';

function App() {

  const [coinOfTheDay, getCoinOfTheDay] = useState('');

  useEffect(() =&amp;gt; {
    CryptoAPI.getCoinOfTheDay().then(res =&amp;gt; {
      getCoinOfTheDay(res);
    });
  }, []);

  return (
    &amp;lt;div className='App'&amp;gt;
      &amp;lt;header className='App-header'&amp;gt;
        &amp;lt;h1&amp;gt;React Crypto Chart&amp;lt;/h1&amp;gt;

        &amp;lt;CoinOfTheDay data={coinOfTheDay}&amp;gt;&amp;lt;/CoinOfTheDay&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your application using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see the Cryptocurrency coin of the day&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bLl0cVG1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igwq2lw6nh6sblobbn1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bLl0cVG1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igwq2lw6nh6sblobbn1a.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will continue to extend this application to display Graphical Charts related to Cryptocurrencies and related information. &lt;/p&gt;

&lt;p&gt;Your suggestions are always welcome.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>axios</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Basic Principles of Redux</title>
      <dc:creator>Hardeep Singh</dc:creator>
      <pubDate>Sat, 01 Aug 2020 20:58:02 +0000</pubDate>
      <link>https://forem.com/hardeepbhandal/basic-principles-of-redux-6n9</link>
      <guid>https://forem.com/hardeepbhandal/basic-principles-of-redux-6n9</guid>
      <description>&lt;p&gt;Redux is a state managing library used in JavaScript applications. It manages the state of your application or in simple words, it is used to manage the data of the application. &lt;/p&gt;

&lt;p&gt;It is used with a library like React etc. &lt;/p&gt;

&lt;p&gt;In this article we will cover three basic principles of Redux.&lt;/p&gt;

&lt;h1&gt;
  
  
  The first principle of Redux
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;You represent whole state of your application as a single JavaScript object&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;State example for a counter application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    CounterValue: 0
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your application may be a simple one like counter example, TodoList example or a complex application with a lot of UI, and change of state, you are going to represent the whole state of your application as a single JavaScript object.&lt;/p&gt;

&lt;h1&gt;
  
  
  The second principle of Redux
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;State tree is read only. You cannot modify or write to it.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;How you change the state?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anytime you want to change the state, you need to dispatch an action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Action?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An action is a plain JavaScript object describing the change. Example increment counter value or decrement counter value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
       type: 'INCREMENT'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
       type: 'DECREMENT'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pure vs Impure Functions
&lt;/h2&gt;

&lt;p&gt;To understand this principle we will have to differentiate between pure and impure functions. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pure functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pure functions do not have any network or database calls.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They just calculate the new value.&lt;/li&gt;
&lt;li&gt;If you call the pure function with the same set of arguments or parameters, you’re going to get the same returned value.&lt;/li&gt;
&lt;li&gt;Pure functions are predictable.&lt;/li&gt;
&lt;li&gt;They do not modify the values passed to them, see following example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function cube(x) {
  return x*x*x;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Impure functions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Any function that changes the internal state of one of its arguments or the value of some external variable is an impure function . They may have any side effects like network or database calls and it may modify the arguments which are passed to them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getSquare(items) {
  var len = items.length;
  for (var i = 0; i &amp;lt; len; i++) {
    items[i] = items[i] * items[i];
  }
  return items;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;These functions may call the database or the network,&lt;/li&gt;
&lt;li&gt;They may operate on the DOM,&lt;/li&gt;
&lt;li&gt;They may override the values that you pass to them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The third principle of Redux
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;To describe state mutations/changes, you have to write a function that takes the previous state of the app, the action being dispatched, and returns the next state of the app.&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This function has to be pure.&lt;/li&gt;
&lt;li&gt;This function is called the “Reducer.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bKk5crJy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ajiy2h501p5s0tt753oi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bKk5crJy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ajiy2h501p5s0tt753oi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.techbrainshub.com/technology/redux/basic-principles-of-redux/"&gt;Complete Article link and more related to Redux and React&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
