<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akarsh Srivastava on Medium]]></title>
        <description><![CDATA[Stories by Akarsh Srivastava on Medium]]></description>
        <link>https://medium.com/@akrsh24-srivastava?source=rss-23968af2d043------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*k9tALds1F4AGx33SL9MIDQ.jpeg</url>
            <title>Stories by Akarsh Srivastava on Medium</title>
            <link>https://medium.com/@akrsh24-srivastava?source=rss-23968af2d043------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:30:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akrsh24-srivastava/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setup a basic ElasticSearch Pipeline with Logstash and Kibana]]></title>
            <link>https://medium.com/swlh/setup-basic-elasticsearch-pipeline-with-logstash-and-kibana-37940aa1883b?source=rss-23968af2d043------2</link>
            <guid isPermaLink="false">https://medium.com/p/37940aa1883b</guid>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[logstash]]></category>
            <category><![CDATA[elastic-stack]]></category>
            <category><![CDATA[kibana]]></category>
            <dc:creator><![CDATA[Akarsh Srivastava]]></dc:creator>
            <pubDate>Mon, 11 Apr 2022 11:27:11 GMT</pubDate>
            <atom:updated>2022-04-11T11:28:01.857Z</atom:updated>
            <content:encoded><![CDATA[<p>Every application/API service generates logs that need to be monitored and analyzed for analytics purposes. Out of all the monitoring techniques, ELK (ElasticSearch, Logstash, and Kibana) is one of the best for monitoring, logging, and log analysis.</p><p>Let’s start with the setup and installation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vFHx1kAsPmRn9flg3YoMag.png" /></figure><p><strong><em>Installation:</em></strong></p><ol><li><em>ElasticSearch</em>:</li></ol><p>Windows users can download the installer zip from here: <a href="https://www.elastic.co/downloads/elasticsearch">Link</a>. While macOS users can install it via Homebrew.</p><ul><li>To install with Homebrew, you first need to tap the Elastic Homebrew repository:</li></ul><pre>brew tap elastic/tap</pre><ul><li>Once you’ve tapped the Elastic Homebrew repo, you can use the below command to install the <strong>latest version</strong> of Elasticsearch.</li></ul><pre>brew install elastic/tap/elasticsearch-full</pre><ul><li>Once it’s done, run elasticsearch from anywhere:</li></ul><pre>elasticsearch</pre><ul><li>This will run elasticsearch on <a href="http://localhost:9200/">http://localhost:9200/</a>, with content something like this.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0dVNQcM6dWomNtPQ1m0byw.png" /><figcaption>Elasticsearch</figcaption></figure><p>2. <em>Kibana</em></p><p>Windows users can download Kibana from here: <a href="https://www.elastic.co/downloads/kibana">Link</a>. While macOS can do it via Homebrew.</p><ul><li>Install the latest version of Kibana by running:</li></ul><pre>brew install elastic/tap/kibana-full</pre><ul><li>Once it’s done, run kibana from anywhere:</li></ul><pre>kibana</pre><ul><li>This will run kibana on <a href="http://localhost:5601/">http://localhost:5601/</a>, with content something like this.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fheV7ZXMryv88aZ4B51sg.png" /><figcaption>Kibana</figcaption></figure><p>3. <em>Logstash</em></p><p>Download Logstash from here: <a href="https://www.elastic.co/downloads/logstash">Link</a>.</p><ul><li>Once the download completes, unzip the download file and move inside the folder.</li></ul><pre>cd logstash-8.1.1/</pre><blockquote><strong>Logstash configuration file</strong></blockquote><ul><li>To configure Logstash, you need to create a config file that specifies which plugins you want to use and the settings for each plugin. You can learn more about how we make it, and the different plugins and filters we can use to parse required data from the logs here: <a href="https://www.elastic.co/guide/en/logstash/current/config-examples.html">Link</a>. Here is a simple example of a logstash config file, where we are using the “grok” filter to get different elements from each log generated.</li><li>For reference purposes, our log file contains logs in this format.</li></ul><p><em>Log format</em>: Timestamp — Function-name — Level — Message</p><pre><em>2022-03-11</em> <em>22:29:01,864</em> - app-logger - INFO - Get data1</pre><pre><em>2022-03-12</em> <em>22:30:01,864</em> - app-logger - INFO - Get data2</pre><pre><em>2022-03-13</em> <em>22:31:01,864</em> - app-logger - INFO - Get data3</pre><pre><em>2022-03-14</em> <em>22:32:01,864</em> - app-logger - INFO - Get data4</pre><pre><em>2022-03-15</em> <em>22:33:01,864</em> - app-logger - INFO - Get data5</pre><pre><em>2022-03-16</em> <em>22:34:01,864</em> - app-logger - INFO - Get data6</pre><ul><li>The configuration file will look something like this:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25c58890e4e6b8bfc7d45bcf44ef65c8/href">https://medium.com/media/25c58890e4e6b8bfc7d45bcf44ef65c8/href</a></iframe><ul><li>Grok is a great way to parse unstructured log data into something structured and queryable. Learn more about grok filter, <a href="https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html">here</a>.</li><li>Here, we have made each element of a log queryable.</li><li>Now, run logstash by copying this config file inside &lt;logstash-directory&gt;/conf/.</li></ul><pre>bin/logstash -f conf/logstash.conf</pre><ul><li>Logstash will start and start generating logs like this. Each of the keys you see is queryable. (timestamp, log-level, logMessage, type).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*puSkxYughMPP1bTIrVz0vA.png" /></figure><p>So, we have successfully installed and setup a ELK pipeline. You can view logs on Kibana level by :</p><ul><li>Create an index pattern by going through <a href="http://localhost:5601/app/management/kibana/indexPatterns">http://localhost:5601/app/management/kibana/indexPatterns</a>.</li><li>Click on “Create Index Pattern” button on the top right side of the screen and add a new index pattern that matches the index you have mentioned in the logstash config file.</li><li>After creating , switch to <a href="http://localhost:5601/app/discover">http://localhost:5601/app/discover</a> and select the created index pattern. You will start seeing all the logs now.</li><li>You can create dashboard with different visualisations like table, area and other graphs.</li></ul><p>That’s it from the setup and installation part of ELK stack. In the next part of this article, we will cover setting up a pipeline to ingest GKE logs into ELK stack.</p><p>If you find this article interesting and helpful, do give some claps and please share it with your friends. Stay tuned for the next part.</p><p>References:</p><ul><li><a href="https://www.elastic.co/guide/index.html">https://www.elastic.co/guide/index.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37940aa1883b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/setup-basic-elasticsearch-pipeline-with-logstash-and-kibana-37940aa1883b">Setup a basic ElasticSearch Pipeline with Logstash and Kibana</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL with MongoDB and ExpressJS]]></title>
            <link>https://medium.com/geekculture/graphql-with-mongodb-and-expressjs-26e1b94ab886?source=rss-23968af2d043------2</link>
            <guid isPermaLink="false">https://medium.com/p/26e1b94ab886</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <dc:creator><![CDATA[Akarsh Srivastava]]></dc:creator>
            <pubDate>Mon, 17 May 2021 04:02:24 GMT</pubDate>
            <atom:updated>2021-05-18T20:58:58.491Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAsysNrEa3PpM18l1CvL0w.jpeg" /><figcaption>Pic Courtesy: <a href="https://www.multidots.com/graphql-efficient-alternative-rest/">Multidots</a></figcaption></figure><p>GraphQL, or Graph Query Language, is a query language for your API. It gives you the power to :</p><blockquote>Ask for what data you need, and you get exactly that<br>- Get many resources in a single request<br>- Add new fields and evolve your API without versions.</blockquote><blockquote>References: <a href="https://graphql.org/">GraphQL</a></blockquote><p>You can look at the official documentation to learn more about GraphQL features.</p><p>Now, let’s talk about the implementation. We will be creating an express-graphql server with MongoDB to persist the data.</p><p>Let’s get started,</p><ul><li>Create a package.json by running,</li></ul><pre>npm init</pre><ul><li>Install dev dependencies,</li></ul><pre>npm i --save-dev babel-cli babel-preset-env babel-preset-stage-0 @types/node</pre><ul><li>Install project dependencies,</li></ul><pre>npm i --save apollo-server-express express graphql mongodb mongoose nodemon</pre><blockquote><strong>apollo-server-express</strong>: This is the Express and Connect integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks.</blockquote><blockquote><strong>express</strong>: NodeJS web application framework.</blockquote><blockquote><strong>graphql</strong>: The JavaScript reference implementation for GraphQL.</blockquote><blockquote><strong>mongodb</strong>: The official MongoDB driver for Node.js.</blockquote><blockquote><strong>mongoose</strong>: Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.</blockquote><blockquote><strong>nodemon</strong>: nodemon is a tool that helps develop node. js-based applications by automatically restarting the node application when file changes in the directory are detected.</blockquote><p>After installing all the dependencies, your package.json should look like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36a3c2e7b41fca18248e1a8750a16b2f/href">https://medium.com/media/36a3c2e7b41fca18248e1a8750a16b2f/href</a></iframe><ul><li>Now, if package.json is ready, create a .babelrc configuration file under the root folder with presets,</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74790dc191f5462057f02f59386a9ac2/href">https://medium.com/media/74790dc191f5462057f02f59386a9ac2/href</a></iframe><ul><li>For graphql support, add a graphql.config.js file, under the root folder with content, this will add support and highlights graphql syntax.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84b8e6c8dd637be87bbc94c5b2a74f3c/href">https://medium.com/media/84b8e6c8dd637be87bbc94c5b2a74f3c/href</a></iframe><p>Now, let’s add a config file for the application and the MongoDB connection string. We can see the below file in this we have defined server port and the DB connection string.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7f78a84e1e03298bbcfa85a7d83ac20/href">https://medium.com/media/f7f78a84e1e03298bbcfa85a7d83ac20/href</a></iframe><p>Now, we will define different schemas for MongoDB using the mongoose tool. In analogous to Relational DB, the schema contains the structure of the various tables we will be creating in our DB.</p><p>We will be creating two schemas for this demo.</p><ol><li>FriendsSchema (Info about his/her friends)</li><li>SeriesSchema (TV series schema).</li></ol><blockquote>Compile the .ts file before running the application to transpile them to the .js file.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7d26062e0c6006ac99828d00f1c043e/href">https://medium.com/media/d7d26062e0c6006ac99828d00f1c043e/href</a></iframe><p>As we have defined various DB schemas, now we can initialize our MongoDB connection.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af391a35a28e635488108da0ab321c65/href">https://medium.com/media/af391a35a28e635488108da0ab321c65/href</a></iframe><blockquote>This file configures the MongoDB connection, with required environment variables, connection string, and schemas registered to the DB.</blockquote><p>Till now, we have initialized our application, installed dependencies, added DB support. Now we will move to the main task of configuring the GraphQL instance.</p><ol><li>We will first define the graphql schema, using the graphql-query-language(gql) tool from <a href="https://www.apollographql.com/docs/apollo-server/">apollo-server-express</a>. The graphQL server uses a <strong>schema</strong> to describe the shape of your data graph. This schema defines a hierarchy of <strong>types</strong> with fields that are populated from your back-end data stores. The schema also specifies exactly which <strong>queries</strong> and <strong>mutations</strong> are available for clients to execute against your data graph.</li></ol><blockquote>Here, we have defined different types along with Query and Mutation entry points.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3aecf0271645570ee2fb70d9839d4b0/href">https://medium.com/media/f3aecf0271645570ee2fb70d9839d4b0/href</a></iframe><p>2. Now, we will define the GraphQL resolvers. The server needs to know how to populate data for every field in your schema so that it can respond to requests for that data. To accomplish this, it uses resolvers.</p><blockquote><strong>A resolver is a function responsible for populating the data for a single field in your schema.</strong> It can populate that data in any way you define, such as fetching data from a back-end database or a third-party API.</blockquote><p>Here, we can see different types of resolvers for QueryType and Mutation Types. These are the same functions, types defined in the schema.graphql file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/686f7b9d5e611f111d4ddd942e3b279b/href">https://medium.com/media/686f7b9d5e611f111d4ddd942e3b279b/href</a></iframe><p>If we have followed up till now with all the configurations, we are successful in initializing, setting up our MongoDB, GraphQL Apollo Server.</p><p>Now the last piece of our puzzle remaining is the express-server to be initialized. This server will be the entry point of our whole server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ecf0b330c26c307835c1cd16ff470e3/href">https://medium.com/media/2ecf0b330c26c307835c1cd16ff470e3/href</a></iframe><p>With this, we have completed our setup task with all the configurations. Now, it’s time to run the server.</p><p>Try to install the dependencies, if not, by running the below command.</p><pre>npm install<br>OR<br>yarn</pre><p>Now run the server by running</p><pre>npm start</pre><p>You will see the server will start running on <a href="http://localhost:8080/graphql">http://localhost:8080/graphql</a>. It will open a GraphQL playground. (Apollo Server hosts GraphQL Playground automatically when you run it in a non-production environment). Learn more about this <a href="https://www.apollographql.com/docs/apollo-server/getting-started/#step-8-execute-your-first-query">GraphQL playground</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BKrlIkXFOM5tG-AB_co6jg.gif" /><figcaption>GraphQL Apollo Server Playground</figcaption></figure><p>With this, we conclude this article. You can check out the complete codebase in the below Github Repository.</p><p>Do star the repository to get future updates over GraphQL.</p><p>Also, give a clap if you like this article, and this helped you in your GraphQL server setup. 😊</p><ul><li><a href="https://github.com/akrsh24/GraphQL-Medium">akrsh24/GraphQL-Medium</a></li><li><a href="https://www.apollographql.com/docs/apollo-server/">Introduction to Apollo Server</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26e1b94ab886" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/graphql-with-mongodb-and-expressjs-26e1b94ab886">GraphQL with MongoDB and ExpressJS</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does Javascript work?]]></title>
            <link>https://medium.com/swlh/how-does-javascript-work-e2ee58e8a2f8?source=rss-23968af2d043------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2ee58e8a2f8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-fundamental]]></category>
            <category><![CDATA[call-stack]]></category>
            <category><![CDATA[execution-context]]></category>
            <dc:creator><![CDATA[Akarsh Srivastava]]></dc:creator>
            <pubDate>Thu, 10 Dec 2020 17:19:53 GMT</pubDate>
            <atom:updated>2021-05-22T19:03:13.944Z</atom:updated>
            <content:encoded><![CDATA[<p>Being a javascript user, you may have encountered some of the basic concepts like hoisting, closure, scope, and all. But, before we learn and understand these concepts, we need to understand the primary process that works in the background, and we always miss to understand.<br>This concept will give you the idea of the inner working of how a Javascript code compiles and run in a web browser/node server.</p><p>So, let introduce this topic of discussion that helps in understanding the working of Javascript code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7qluK7COPPCmVIhwvkTmxQ.png" /><figcaption>Execution Context</figcaption></figure><h3><strong>Execution Context</strong></h3><p>Execution context is the place where actually everything inside JS executes. Every javascript code carries out in an execution context only. <br>At the top of any javascript code, it creates an Execution context, which will be global throughout the JS code.<br>So, before we move further, let’s see and discuss how execution context helps in the working of a Javascript code.</p><p>To understand the execution context, you can treat it as a block of context that lets the compiler know about all the variables, functions declared in that context.</p><p>In simple words, the creation of execution context takes place in 2 phases-</p><ol><li>Memory creation phase</li><li>Code execution phase</li></ol><p>So, execution context can be said to set a context that first does all the fetching of variables &amp; functions local to that context, and then does the code execution after that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UQPF6rO6KVIsGRXovMVIyw.png" /></figure><p>There is one more thing to be discussed which supports and maintains the order of the execution of execution contexts.</p><h3>Call Stack</h3><p>As for now, we will not cover the call stack in deep, but you can infer from its name, that it’s a stack.</p><p>Whenever the engine asked to create a new execution context, it first pushes the execution context reference into the call stack and pops it out when the code inside the context completes. Once the execution context pops out from the call stack, that execution context gets deleted completely, with no access to that context’s variables and functions defined. There is some slight variation in this in the concept of closures, but right now, we will ignore closure.</p><p>So what we infer is,</p><ol><li>Call stack manages the order of execution of the execution context.</li><li>With each execution context creation and deletion, the JS engine pushes and pops out it from the call stack.</li><li>Global execution context automatically becomes the first context to be pushed and the last context to be popped out from the call stack.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*Nfx4eQVGvXP1w9-YEiFchA.png" /><figcaption>Call Stack for execution context</figcaption></figure><p>Let’s discuss the same with a simple code example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YO_6Rrc-OgbrR0NxMkGWgA.png" /><figcaption>Simple javascript code with variables and functions</figcaption></figure><p>So, in the above code snippet, it’s a simple JS code with some variables and functions defined and executed.</p><p>At the end of this section, you will get to know what actually works in the background and how the Javascript engine works.</p><p>As we have already discussed in the beginning that the JS engine at the beginning of any JS code creates a global execution context and subsequent execution context as we execute further.</p><p>Let’s see how the above code executes with each execution context:</p><ul><li>At first, Global Execution Context to be created. This first gets pushed to the call stack.</li></ul><ol><li><strong>Memory Creation phase:</strong></li></ol><p>In the memory creation phase, all the local scope variables(in this, case the global scope), functions are recognized and thus given memory.</p><p>It will look something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fjz4nY4EcOkbPdoy1Bzh1g.png" /><figcaption>Memory creation phase of variables and functions</figcaption></figure><blockquote>Variables are initially initialized <strong>undefined, </strong>while normal functions are given the complete <strong>function definition, </strong>the named function constants are initialised undefined, just like the variables are.</blockquote><p>2. <strong>Code Execution phase:</strong></p><p>Once the memory creation phase completes, we move over to the code execution phase. Here, the engine does the code running, value initialization, the function call, and all. Let’s discuss this phase for the above example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJ4LvWsEm0oLFJcMsXZOww.png" /></figure><ol><li>When the code executor reaches line 1, it sees</li></ol><blockquote>var a=2;</blockquote><p>Here the engine asks the scope, do we have the memory declaration of the variable a. If it finds in the memory, it initializes the value 2 to a.</p><p>2. As it moves to line 2, it sees the function definition, and it moves further.</p><p>3. After this, it reaches line 7, where it sees value initialization of variable b.</p><blockquote>var b=5;</blockquote><p>It does the same behavior of asking the scope to find the memory declaration and initializes the value 5 to b.</p><p>4. Once it reaches line 8, here the engine sees a function call, and this leads to the creation of a new execution context local to the function called, and this context’s reference to being pushed into the call stack.</p><p>Now call stack contains two element — Global execution context and getTwiceTheNumber() execution context.</p><p>So, here we will discuss the new execution context created, with the call of function getTwiceTheNumber().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jxjWLVRXvr_C3NTSc3JEcA.png" /></figure><ol><li>Memory creation phase:</li></ol><p>Here also, just the global context, all the local scope variables to the function will be fetched and initialized to undefined.</p><p>2. Code creation phase:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yjCoMsLFlrmlgfYda_YB0w.png" /></figure><ol><li>So, now the engine reaches inside of the function and executes its first line of code. Here it sees —</li></ol><blockquote>let double = num;</blockquote><p>It will ask the scope of the variable <strong>double</strong> is there in their local memory scope, if it exists the value num*2 (num=2) assigned to it.</p><p>2. Now, it will go to line 3, console.log the variable double.</p><p>3. In line 4, it seems that there is a return statement, which also signifies the end of the function scope. It returns the value to the function call line, and here,</p><ul><li>The execution context scope ends. It gets popped up from the call stack.</li><li>The execution context scope gets deleted.</li></ul><p>5. So, we are back again to line 9, which similarly calls the function getTwiceTheNumber(). Here new execution context to be created as we saw in the above section. It will be pushed into the call stack and popped when completed</p><p>6. Once line 9 has completed execution, there will be no line of code to process more, and here the global context code execution also completes, and it gets popped out from the call stack, leaving the call stack empty.</p><blockquote>So this is how the Javascript engine works in the background.<br>- The compiler does the variable &amp; function memory creation.<br>- The Engine does the code execution.<br>- The scope sees the context of the variables exists or not.</blockquote><p>This concept is the first step to understand further concepts like hoisting, closures, and all.</p><p>We will cover these concepts in some other story.</p><p>Do give a clap, if I was able to help you in any way to understand this concept.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2ee58e8a2f8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-does-javascript-work-e2ee58e8a2f8">How does Javascript work?</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ReactJS With Redux.]]></title>
            <link>https://medium.com/swlh/reactjs-with-redux-293fa171e3f9?source=rss-23968af2d043------2</link>
            <guid isPermaLink="false">https://medium.com/p/293fa171e3f9</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-redux]]></category>
            <dc:creator><![CDATA[Akarsh Srivastava]]></dc:creator>
            <pubDate>Mon, 28 Sep 2020 07:18:21 GMT</pubDate>
            <atom:updated>2020-09-29T08:55:13.748Z</atom:updated>
            <content:encoded><![CDATA[<p>As one starts building his application in ReactJS, one needs to keep an eye on the complexity of application state management. As the application state tree grows with more nodes, the complexity to manage them also increases widely. So, we need a state management utility to manage our states effectively. Here will be using Redux, a library coupled with our ReactJS application for state management.</p><figure><img alt="React with Redux" src="https://cdn-images-1.medium.com/max/327/1*HRqcxwqyRj3OGXjj92niog.png" /></figure><p><strong>Pre-Requisite:</strong></p><ol><li>The package manager (npm, yarn) is installed in your system. (Download it from here, if not installed <a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a> and follow the steps as guided).</li><li>Have some prior basic knowledge of Javascript and HTML.</li></ol><p>So, we’ll first start with creating a new React project from scratch, and then we’ll integrate Redux into the application.</p><p><strong>Create a new React App :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n7V4Kv9IKpf_UQfxsMsmEA.jpeg" /><figcaption>React</figcaption></figure><ol><li>We will be using <strong>create-react-app (CRA)</strong>, it just creates a frontend build pipeline, with all the necessary initial files ready for use.</li></ol><blockquote><em>npx create-react-app &lt;app-name&gt;</em></blockquote><blockquote><em>e.g: npx create-react-app first-app</em></blockquote><p>2. This will create a readymade development environment with all the required dependencies installed and initial codebase to start with, as illustrated below.</p><figure><img alt="react-app-code-structure" src="https://cdn-images-1.medium.com/max/664/1*ZCIqGr-cAUf5l3hXDF43SQ.png" /></figure><p>We’ll be working under the src/ folder of my project directory.</p><p>3. Directories description -</p><blockquote>a. <strong>public</strong>/ — It’s the root folder that gets dealt by the web server in the end. It contains all the files, which are present as it is when the application is built for production. It also contains the index.html file, which is the root HTML file of the application.</blockquote><blockquote>b. <strong>src</strong>/ — It’s the root folder for development purposes. All the dev files are present inside this folder.</blockquote><blockquote>c. <strong>node_modules</strong>/ — It contains all the dependencies installed for the application.</blockquote><p>4. src/index.js acts as the script file for the public/index.html file.</p><p>5. For development purposes, we will be using a root js file, preferably App.js.</p><p><em>So, for demo purposes, we will be using an online dummy server like </em><a href="https://jsonplaceholder.typicode.com/posts"><em>https://jsonplaceholder.typicode.com/posts</em></a><em>, and we will be fetching and displaying the data on the screen just as shown below.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2eSlPv1ttq1sATnGbCOlA.png" /><figcaption>Simple “All-Posts” to show React-Application.</figcaption></figure><p><strong>Creating a new React application without Redux setup:</strong></p><ol><li>We will be having a code directory structure like this. We have removed non-essential files and created a new component — Posts.jsx for showing the fetched posts.</li></ol><figure><img alt="React without redux code director" src="https://cdn-images-1.medium.com/max/566/1*sBQJIHTLynvGZ7V44U_ggQ.png" /></figure><p>2. We will be rendering all the post on the screen, so we separated that logic in a separate component, and called the component on our parent component, App.js.</p><p>3. First, we will be creating a class component and calling it from the parent component. So, create a new file under the components/ folder with the name as<strong> Posts.jsx</strong>. This file will be having all the component logic to show All Posts on the screen.</p><p>4. So Posts.jsx will be containing the below logic, so render the All Posts section.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7f8c8da912b8057c8ab6f6fc7a64001/href">https://medium.com/media/b7f8c8da912b8057c8ab6f6fc7a64001/href</a></iframe><blockquote>Here, we have created a div which will be showing all the posts on the click of a “Get all Posts“ button . On the click of the button, it will start fetching response data from the given url. As in fetch api, GET is the default method, it will fetch the response, convert it to JSON format and then set in the posts state.</blockquote><p>5. We can have inline styles in this, but it’s better to prefer the external style files for reusability and all. So the external style file, App.css has the below logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/089cd56f4416f69e518702983d726212/href">https://medium.com/media/089cd56f4416f69e518702983d726212/href</a></iframe><p>6. Now, as we have our Posts components ready, it’s time to call it for our use. So, inside the App.js file, we will be calling the Posts component, like this.</p><pre>import React from &#39;react&#39;;<br>import &#39;./App.css&#39;;<br>import Posts from &#39;./components/Posts&#39;;</pre><pre>function App() {<br> return (<br>     &lt;div className=&quot;App&quot;&gt;<br>       &lt;Posts /&gt;<br>     &lt;/div&gt;<br>  );<br>}<br>export default App;</pre><p>And App.css with the following style logic,</p><pre>body,<br>html {<br>  height: 100%;<br>  margin: 0;<br>  padding: 0;<br>  background-color: black;<br>}</pre><blockquote>By this, we will be able to create a simple basic React application without any state management library required. But in this example, it was just one state to manage, but as we move forward with more components, we need one states store-house to retrieve and manage all the states synchronously from any components. There, we will required to setup a state management to manage complex states together.</blockquote><p><strong>Creating a new React application with Redux setup: (Without Redux Toolkit)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iLIrb-4QZ7bDyvKTuPAxsQ.png" /><figcaption>React-Redux</figcaption></figure><ol><li>So, for this tutorial, we will be building a simple counter application using React and Redux for state management.</li></ol><p>2. Before, we start we have to install redux dependencies for our application.</p><pre><strong>npm install redux</strong>   (for redux-core package)<br><strong>npm install react-redux</strong> (for integrating with react)<br><strong>npm install --save-dev</strong> redux-devtools (for developers use)</pre><p>3. Now if we have installed necessary dependencies, we can look over the redux directory structure.</p><figure><img alt="React-Redux code directory stru" src="https://cdn-images-1.medium.com/max/333/1*73LaBY21aVwqQP5kc3hRiA.png" /><figcaption>React-Redux directory structure</figcaption></figure><p>4. We have created different folders inside our redux directory, before moving further let’s discuss these folders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*A3_NHSBdyWShvBKmlYK-QA.jpeg" /><figcaption>Redux Flow</figcaption></figure><blockquote><a href="https://redux.js.org/basics/store"><strong>Store</strong></a><strong>:</strong></blockquote><blockquote>The store acts as the single source of truth for a react-redux application. It is the object that brings them together. It has the following responsibilities:</blockquote><blockquote>i. Holds the application state.</blockquote><blockquote>ii. Allows access to state.</blockquote><blockquote>iii. Allows state to be updated.</blockquote><p>A redux application should contain only a single-store. All other splittings of data logics can be done by reducer-composition instead of many stores.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/459aca18512dbacafde32bb612bd8e44/href">https://medium.com/media/459aca18512dbacafde32bb612bd8e44/href</a></iframe><p>It is easy to create a store if you have a reducer. We can use <a href="https://redux.js.org/api/combinereducers">combineReducers()</a> to combine several reducers into one.</p><ol><li><a href="https://redux.js.org/api/createstore">createStore</a>, creates a Redux store that holds the complete state tree of your app.</li><li><a href="https://redux.js.org/api/applymiddleware">applyMiddleware</a> is the suggested way to extend Redux with custom functionality. The most common use case for middleware is to support asynchronous actions without much boilerplate code or a dependency. It does so by letting you dispatch <a href="https://redux.js.org/glossary#async-action">async actions</a> in addition to normal actions. eg: redux-thunk.</li><li><a href="https://redux.js.org/api/compose">compose</a> helps in improved readability and convenience. <em>Compose</em> is used when you want to pass multiple store enhancers to the store. Store enhancers are higher-order functions that add some extra functionality to the store. The only store enhancer which is supplied with Redux by default is <em>applyMiddleware.</em></li><li>logger helps in logging redux-actions to the console.</li><li>We have also used redux dev tools for using its functionality for dev. purposes.</li></ol><blockquote><a href="https://redux.js.org/basics/actions"><strong>Actions</strong></a>:</blockquote><blockquote>Actions are payloads of information that send data from your application to your store. They are the <em>only</em> source of information for the store.</blockquote><p>They are plain Javascript objects, that must have a “type” property that indicates the type of action being performed, and a “payload/data” property for the store.</p><p>So, we will be defining our action type for our application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9a1aa7f76ce212656c16c559dc7f0a4/href">https://medium.com/media/d9a1aa7f76ce212656c16c559dc7f0a4/href</a></iframe><p>Here, we have defined what types of actions, we will perform on our Redux application.</p><p>Next, we will define the <a href="https://redux.js.org/basics/actions#action-creators">Action Creators</a>, these are pure functions to create actions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b3aca5dcd1fec89a19185dcc4ce41f2/href">https://medium.com/media/8b3aca5dcd1fec89a19185dcc4ce41f2/href</a></iframe><p>Here, we have used bindActionCreators, they turn an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a <a href="https://redux.js.org/api/store#dispatchaction">dispatch</a> call so they may be invoked directly.</p><p>Normally you should just call dispatch directly on your store instance. If you use Redux with React, <a href="https://github.com/gaearon/react-redux">react-redux</a> will provide you with the dispatch function so you can call it directly, too.</p><p>The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn&#39;t aware of Redux, and you don&#39;t want to pass <a href="https://redux.js.org/api/store#dispatchaction">dispatch</a> or the Redux store to it.</p><p>So, in short, we have bound our action creators with dispatch to call them directly, without complete Redux store to it.</p><blockquote><strong>Reducers</strong>:</blockquote><blockquote>Reducers specify how the application’s state changes in response to <a href="https://redux.js.org/basics/actions">actions</a> sent to the store. The reducer is a pure function that takes the previous state and an action, and returns the next state.</blockquote><pre>(previousState, action) =&gt; nextState</pre><blockquote>Just remember that the reducer must be pure. Given the same arguments, it should calculate the next state and return it. No surprises. No side effects. No API calls. No mutations. Just a calculation.</blockquote><p>So, now we will define the reducer state for our application-</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a74c0bcfe85f8905258555ce7bc3223d/href">https://medium.com/media/a74c0bcfe85f8905258555ce7bc3223d/href</a></iframe><p>Here, as we can see, we have defined pure reducing functions, it takes the previous state using the spread operator, and mutates the required state with the action and gives us the next action.</p><p>Also, as your app grows more complex, you’ll want to split your <a href="https://redux.js.org/glossary#reducer">reducing function</a> into separate functions, each managing independent parts of the <a href="https://redux.js.org/glossary#state">state</a>.</p><p>The combineReducers helper function turns an object whose values are different reducing functions into a single reducing function you can pass to <a href="https://redux.js.org/api/createstore">createStore</a>.</p><p>So, this is how we structure Redux into other React application for state management.</p><p>Next, we will see how we connect the Redux store state and use it in our application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb717a65e51db49a4ab055d031aaa560/href">https://medium.com/media/eb717a65e51db49a4ab055d031aaa560/href</a></iframe><p>To use Redux states into our application, we first need to connect the Redux store with the React application. For this, we will use the <a href="https://react-redux.js.org/api/connect">connect</a>() function.</p><blockquote>The <em>connect()</em> function connects a React component to a Redux store.<br>It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.</blockquote><blockquote>It does not modify the component class passed to it; instead, it returns a new, connected component class that wraps the component you passed in.</blockquote><p>The mapStateToProps and mapDispatchToProps deals with your Redux store’s state and dispatch, respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.</p><p>In simple words, <strong><em>mapStateToProps</em></strong> is the Redux state object, that we want to subscribe to and use in our component. While<strong><em> mapDispatchToProps</em></strong> functions are expected to return an object. Each field of the object should be a function, calling which is expected to dispatch an action to the store.</p><p>As in our application,</p><pre>const mapStateToProps = state =&gt; {    <br>return { count: state.counterReducer.count }<br>}</pre><blockquote>mapStateToProps contains the count Redux state, that we will be using in our application.</blockquote><pre>const mapDispatchToProps = {<br>return {<br>    increaseCounter, <br>    decreaseCounter, <br>    resetCounter<br>}</pre><blockquote>mapDispatchToProps contains the same actionCreators we defined in our action.js code above. These will be available as props in our application to dispatch actions to store.</blockquote><blockquote>For detailed information, you can check out the documentation here, <a href="https://react-redux.js.org/api/connect">https://react-redux.js.org/api/connect</a>.</blockquote><p>Now we have connected our React application with Redux store, mapped store state, and actions to be dispatched as props to the application.</p><p>As in our application, we have 3 buttons, that dispatches actions to the store onClick.</p><ol><li>For incrementing the count, we dispatch the <strong>increaseCounter</strong> with args incrementing count props state by 1.</li></ol><pre>this.props.increaseCounter(this.props.count + 1);</pre><p>2. Similarly, for decrementing counter, we dispatch the <strong>decreaseCounter</strong> with args decrementing count props state by 1, and resetting the counter, dispatching <strong>resetCounter</strong> with args as 0.</p><pre>this.props.decreaseCounter(this.props.count - 1);<br>this.props.resetCounter(0);</pre><p>Hence, our application will react to the redux state changes with each button click. So let’s see it work in action then,</p><figure><img alt="Counter application demo gif" src="https://cdn-images-1.medium.com/max/600/1*u3Qi55aFlVgFJ-KSokNljw.gif" /></figure><p>So, if we cut short the story, actions are the pure functions that tell the reducer to change the state of the application. The store is the storehouse of all this, that brings all this together.</p><h3>Conclusion</h3><p>For smaller application, Redux will not have that much effect, but as our application move to more and more states, it gets complex to manage and flow from one component to another and then it’s a pain to manage state and debug state values in the application state tree. So Redux takes this pain and helps in managing state and it flows.</p><p>You can check out this repo for this article here, you will get both React with Redux and React without Redux demo code.</p><p><a href="https://github.com/akrsh24/React-Redux-Medium">akrsh24/React-Redux-Medium</a></p><p>For more detailed information on the Redux and it’s API’s usage, check out the official documentation.</p><ul><li><a href="https://redux.js.org/">Redux - A JS library for predictable and maintainable global state management | Redux</a></li><li><a href="https://react-redux.js.org/">React Redux | React Redux</a></li></ul><p>Do give your comments, suggestion, post your doubts here or reach out to me for any queries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=293fa171e3f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/reactjs-with-redux-293fa171e3f9">ReactJS With Redux.</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ant Design: Implementing Pagination in ReactJS.]]></title>
            <link>https://akrsh24-srivastava.medium.com/ant-design-implementing-pagination-in-reactjs-7c2aa5b21dd8?source=rss-23968af2d043------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c2aa5b21dd8</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[ant-design]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Akarsh Srivastava]]></dc:creator>
            <pubDate>Sat, 16 May 2020 20:31:06 GMT</pubDate>
            <atom:updated>2020-12-08T14:55:01.783Z</atom:updated>
            <content:encoded><![CDATA[<p>For the last few months, I and my senior were working on a Web Application built in ReactJS and Ant design CSS Framework. We were representing our data in a catalogic/tiles way, and it was creating sort of an infinite scroll with each increasing data. So thought of implementing a page view for the app data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWEAIVqiGTKDxOLg1-bckA.png" /><figcaption>The similar design we were trying to do for our data. (Courtesy- Hotstar)</figcaption></figure><h3>Ant Design</h3><p>A design system for enterprise-level products. Create an efficient and enjoyable work experience.</p><h3>Installation:</h3><pre>$ npm install antd</pre><pre>$ yarn add antd</pre><p>After adding the dependency:</p><blockquote>import { Pagination } from ‘antd’;</blockquote><h3><strong>Set React initial states:</strong></h3><p><strong><em>elementsPerPage</em></strong> is the number of elements you want to render on a single page. <strong><em>currentPageElements</em></strong> array is to store the elements to render for each page and <strong><em>offset</em></strong> helps to select those elements. <strong><em>pagesCount</em></strong> sets the total number of pages to render.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffa7d3540ba90affdb0b0b9cfc436d7b/href">https://medium.com/media/ffa7d3540ba90affdb0b0b9cfc436d7b/href</a></iframe><p><strong>Implementing Pagination with fetched Data:</strong></p><p>In the below code snippet, we are fetching data to be rendered catalogic with pagination. <strong>getAllElements</strong> is an async function that will fetch data from a REST API and store it in the <strong><em>allElements</em></strong> state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b843afa7936db263dbab1f472818357b/href">https://medium.com/media/b843afa7936db263dbab1f472818357b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51743bee355c65fe6be34ba5f0b5ba79/href">https://medium.com/media/51743bee355c65fe6be34ba5f0b5ba79/href</a></iframe><p>In this, we have implemented a function to set pageCount state, which stores the number of pages to render, with a callback function to set elements for the current page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bc609a01a79b4f8f3302c4c97494bce/href">https://medium.com/media/1bc609a01a79b4f8f3302c4c97494bce/href</a></iframe><p>Here, we have implemented a function to calculate elements for the current page depending upon elementsPerPage and offset state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61a937f8c62a9409222dd16e75d36463/href">https://medium.com/media/61a937f8c62a9409222dd16e75d36463/href</a></iframe><p>Here, to render the pagination page list, we have used the antd “P<a href="https://ant.design/components/pagination/">agination</a>” component with the required props. <strong><em>defaultCurrent </em></strong><em>is used to indicate the default page, </em><strong><em>handlePageClick</em></strong> is the handler to<strong><em> </em></strong>set elements for each page(defined below), <strong><em>size</em></strong> of the pagination list, the <strong><em>total</em></strong> is the count of elements, <strong><em>showTotal</em></strong> is the string indicating the range of elements in the current page. <strong><em>pageSize</em></strong> is the number of elements on each page. The page list component appears if and only if we have to render more than one page as coded in the above logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72fdb3ec5b7af1072c9c93a8844b1cb5/href">https://medium.com/media/72fdb3ec5b7af1072c9c93a8844b1cb5/href</a></iframe><p><strong><em>handlePageClick</em></strong> is the handler function that calculates the offset of the current page and calls the callback function setElementsForCurrentPage to set elements for the current changed page.</p><p>Now just, return the state variable <strong>currentPageElements</strong> to show the elements in any way you want, with pagination.</p><p>I usually use the “<a href="https://ant.design/components/card/">Card</a>” component of the antd library. You can use the bootstrap card or any card type layout for your design.</p><p>For eg:</p><pre>showCatalogicData = () =&gt; {<br>    return this.state.currentPageElements.map(data =&gt; {<br>        return (<br>            &lt;Card<br>              key={data.id}<br>              className=&quot;card-item&quot; <br>              style={{ width: 300 }}<br>            &gt;<br>               &lt;Meta title={data.Title}/&gt;<br>             &lt;/Card&gt;<br>          )<br>       });<br>}</pre><figure><img alt="Resultant page list" src="https://cdn-images-1.medium.com/max/470/1*j8HDC4YNuhnoDCMe0eNl6g.png" /><figcaption>Result of pagination list rendering with the page size of 4.</figcaption></figure><p>That’s how we can implement pagination on a React-App. You can check out this repository for complete code. Do star the repository for any future updates.</p><p><a href="https://github.com/akrsh24/Pagination-Ant-design-Medium">akrsh24/Pagination-Ant-design-Medium</a></p><p>Check out these links for understanding the Ant design CSS framework.</p><ul><li><a href="https://ant.design/">Ant Design - The world&#39;s second most popular React UI framework</a></li><li><a href="https://ant.design/components/pagination/#header">Pagination - Ant Design</a></li></ul><p>Do give a clap if you like this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c2aa5b21dd8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>