<?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>DEV Community: Aman Kumar Singh</title>
    <description>The latest articles on DEV Community by Aman Kumar Singh (@amankumarsingh01).</description>
    <link>https://dev.to/amankumarsingh01</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%2F463224%2Fe13115fc-323a-439b-91b8-e03a5702a929.jpg</url>
      <title>DEV Community: Aman Kumar Singh</title>
      <link>https://dev.to/amankumarsingh01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amankumarsingh01"/>
    <language>en</language>
    <item>
      <title>React native and the JavaScript bridge.</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Sat, 13 Apr 2024 06:03:16 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/react-native-and-the-javascript-bridge-3ipj</link>
      <guid>https://dev.to/amankumarsingh01/react-native-and-the-javascript-bridge-3ipj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article we will discuss how does react native came in existence, what problems it solves.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is react native?
&lt;/h2&gt;

&lt;p&gt;React native is a framework build by facebook and open-sourced in 2015. It is a JavaScript based mobile app framework, that allows to build natively rendered mobile app for Android and IOS.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;wait for both android and ios form single js code how?&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The answer is "JavaScript bridge". Or in other words "BLACK MAGIC".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today i will try to unveil, how react native does it.&lt;/p&gt;

&lt;p&gt;At first lets assume that React native might be compiling JavaScript code into native code that can be executed on IOS and Android respectively.&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%2Fso6jpmish4t7m3us1s8h.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%2Fso6jpmish4t7m3us1s8h.png" alt="Diagram for above assumed paradigm" width="800" height="584"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But this analogy is extremely hard to achieve as Objective C and Java are strongly typed languages, while JavaScript is not, so react native does not follow this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's understand JavaScript Bridge.
&lt;/h2&gt;

&lt;p&gt;As you go through the React native Documentation you will see this word "JavaScript Bridge" and this is responsible for the Magic/Black-magic done by react native. &lt;/p&gt;

&lt;p&gt;Let's compress the picture, to execute the JavaScript code on native devices there must be some way that the JS thread can communicate with the Native thread at the runtime, this is done by  JavaScript bridge it acts as a messenger/postman between the JS thread and Native Thread.&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%2Fdc9dpbus12a6f8ojmakg.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%2Fdc9dpbus12a6f8ojmakg.png" alt="React native architecture" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let me frame this in layman words :
&lt;/h3&gt;

&lt;p&gt;Lets assume conversation two people having different first language. So they communicate in the language that is understood by both.&lt;/p&gt;

&lt;p&gt;In the same way React Native, for communication between JS Thread and Native Thread we rely on JSON messages, pushed into  the message queue. The first service pushes some commands inside this queue, and the other one has to execute them.&lt;/p&gt;

&lt;p&gt;JS thread sends asynchronous JSON message describing the actions that native side needs to accomplish to render the UI.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>reactnative</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Creating a native module in react native.</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Wed, 16 Mar 2022 18:36:37 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/creating-a-native-module-in-react-native-47bn</link>
      <guid>https://dev.to/amankumarsingh01/creating-a-native-module-in-react-native-47bn</guid>
      <description>&lt;p&gt;I use React native on daily basis and some times i get into an issue for which i need a native solution. In this tutorial i will make a native method in java and i will call it form react native.&lt;/p&gt;

&lt;p&gt;Lets start&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%2F6mi4o0fp8yg1fd9nmds5.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%2F6mi4o0fp8yg1fd9nmds5.png" alt="File structure for creating native module" width="702" height="2202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how your Empty react native Project will look like, here we will create a native module now in android studio.&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%2F9hvcc46wathjc67f6q4x.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%2F9hvcc46wathjc67f6q4x.png" alt="This is how you open your project in android studio" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on Open Existing project &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%2F4tsw8p1jbu0fyhg7hj55.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%2F4tsw8p1jbu0fyhg7hj55.png" alt="Click in existing Project" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select your android folder of react native project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&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%2Fr91x61vfiholzrqu1439.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%2Fr91x61vfiholzrqu1439.png" alt="Create a new Java class" width="800" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new Java Class for timeBeing we will name it &lt;code&gt;HelloPTModule&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;Here we will write some Java code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.whatsappcamera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;android.provider.MediaStore&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;androidx.annotation.NonNull&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;androidx.annotation.Nullable&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.Callback&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.ReactApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.ReactContextBaseJavaModule&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.ReactMethod&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.jetbrains.annotations.NotNull&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloPTModule&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ReactContextBaseJavaModule&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HelloPTModule&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Nullable&lt;/span&gt; &lt;span class="nc"&gt;ReactApplicationContext&lt;/span&gt; &lt;span class="n"&gt;reactContext&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
         &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reactContext&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"HelloPT"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@ReactMethod&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Callback&lt;/span&gt; &lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;Lets understand what is written inside this&lt;/p&gt;

&lt;p&gt;Every Native Module written for React Native to use always extends &lt;code&gt;ReactContextBaseJavaModule&lt;/code&gt;. This class should have a public function &lt;code&gt;getName&lt;/code&gt; which returns a string. Note that this string is important as we will use this identifier to call our native function form react native.&lt;/p&gt;

&lt;p&gt;sayHello is the function which we will invoke form React Native.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2
&lt;/h2&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%2Fok9owvqeekbfqig2hykb.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%2Fok9owvqeekbfqig2hykb.png" alt="HelloPTPackage class creation" width="800" height="932"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we will create another Java class for time being lets Name it &lt;code&gt;HelloPTPackage&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your helloPTPackage will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.whatsappcamera&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.ReactPackage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.NativeModule&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.bridge.ReactApplicationContext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.facebook.react.uimanager.ViewManager&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.ArrayList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Collections&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.List&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloPTPackage&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ReactPackage&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ViewManager&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createViewManagers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ReactApplicationContext&lt;/span&gt; &lt;span class="n"&gt;reactContext&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;emptyList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NativeModule&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createNativeModules&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;ReactApplicationContext&lt;/span&gt; &lt;span class="n"&gt;reactContext&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NativeModule&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;modules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

        &lt;span class="n"&gt;modules&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HelloPTModule&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reactContext&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;modules&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Now we will register our package Open MainApplication, inside getPackages Method&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%2Ffvdp7r74gvythttwh5he.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%2Ffvdp7r74gvythttwh5he.png" alt="MainActivity class things to add" width="800" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;add this line,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="n"&gt;packages&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HelloPTPackage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step marks end of the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  step 5 The Final step
&lt;/h2&gt;

&lt;p&gt;To use this native module you have to import&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NativeModules&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SafeAreaView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;HelloPT&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;NativeModules&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// this is the same name we returned in getName function.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Change&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;HelloPT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;err&lt;/span&gt;&lt;span class="p"&gt;)&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;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&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;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SafeAreaView&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;onPress&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Change&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"call native function"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;SafeAreaView&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you click the button this will be in your terminal&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%2F5ugv1u0soffj37pniqnv.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%2F5ugv1u0soffj37pniqnv.png" alt="YAY!! Terminal Output" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for bearing with me 😊, Hope you enjoyed learning.&lt;br&gt;
Please comment and share among your friends.  &lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>android</category>
      <category>react</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Improving web performance and Lighthouse performance results</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Mon, 29 Mar 2021 11:12:53 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/improving-web-performance-and-lighthouse-performance-results-2fel</link>
      <guid>https://dev.to/amankumarsingh01/improving-web-performance-and-lighthouse-performance-results-2fel</guid>
      <description>&lt;p&gt;From this year google added performance as a key metric to rank your website in google search and ads &lt;a href="https://developers.google.com/web/updates/2018/07/search-ads-speed" rel="noopener noreferrer"&gt;Give it a look&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now the question is what is the criteria lighthouse/google uses to measure performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lighthouse Criterias (please check the image below)
&lt;/h2&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%2Fj2910m80q54p1m7z3gui.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%2Fj2910m80q54p1m7z3gui.png" alt="alt text" width="668" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Out of these things the google mainly focus on these criteria &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;FCP (First Contentful Paint) &lt;/li&gt;
&lt;li&gt;LCP (Largest Contentful Paint) &lt;/li&gt;
&lt;li&gt;CLS (Cumulative Layout Shift)&lt;/li&gt;
&lt;li&gt;Time to interact &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's discuss about these one by one &lt;/p&gt;

&lt;h2&gt;
  
  
  FCP (First Contentful Paint)
&lt;/h2&gt;

&lt;p&gt;According to lighthouse &lt;code&gt;First contentful paint marks the time at which the first text or image is painted.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://web.dev/first-contentful-paint/" rel="noopener noreferrer"&gt;web.dev&lt;/a&gt; &lt;code&gt;FCP measures how long it takes the browser to render the first piece of DOM content after a user navigates to your page. Images, non-white &amp;lt;canvas&amp;gt; elements, and SVGs on your page are considered DOM content; anything inside an iframe isn't included.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lighthouse FCP evaluating criteria&lt;/em&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%2F5mrcds3nadc34cpco96g.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%2F5mrcds3nadc34cpco96g.png" alt="alt text" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to improve FCP
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Deliver your website through CDN.&lt;/li&gt;
&lt;li&gt;Make sure the chunks is small.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  LCP (Largest Contentful Paint)
&lt;/h2&gt;

&lt;p&gt;According to lighthouse &lt;code&gt;Largest Contentful Paint marks the time at which the largest text or image is painted.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://web.dev/lighthouse-largest-contentful-paint/" rel="noopener noreferrer"&gt;web.dev&lt;/a&gt; &lt;code&gt;LCP measures when the largest content element in the viewport is rendered to the screen. This approximates when the main content of the page is visible to users. See Largest Contentful Paint defined for more details on how LCP is determined.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lighthouse LCP evaluating criteria&lt;/em&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%2F55lsejeg1cghqve0ue02.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%2F55lsejeg1cghqve0ue02.png" alt="alt text" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to improve LCP
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Defer the JS.&lt;/li&gt;
&lt;li&gt;Lazy load the images and other assets.&lt;/li&gt;
&lt;li&gt;Responsive image practice prefer using (use srcset).&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Preconnect and preload practices.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  CLS (Cumulative Layout Shift)
&lt;/h2&gt;

&lt;p&gt;According to lighthouse &lt;code&gt;Cumulative layout shift measures the movement of visible elements within the viewport.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://web.dev/cls" rel="noopener noreferrer"&gt;web.dev&lt;/a&gt; &lt;code&gt;CLS measures the sum total of all individual layout shift scores for every unexpected layout shift that occurs during the entire lifespan of the page. A layout shift occurs any time a visible element changes its position from one rendered frame to the next.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lighthouse CLS evaluating criteria&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To calculate the layout shift score, the browser looks at the viewport size and the movement of unstable elements in the viewport between two rendered frames. The layout shift score is a product of two measures of that movement: the impact fraction and the distance fraction (both defined below).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;layout shift score = impact fraction * distance fraction&lt;/code&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%2F79gwk4kl94vse95ryzgt.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%2F79gwk4kl94vse95ryzgt.png" alt="alt text" width="424" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;## Time to interact &lt;/p&gt;

&lt;p&gt;According to lighthouse &lt;code&gt;Time to interactive is the amount of time it takes for the page to become fully interactive.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://web.dev/lighthouse-largest-contentful-paint/" rel="noopener noreferrer"&gt;web.dev&lt;/a&gt; &lt;code&gt;CLS measures the sum total of all individual layout shift scores for every unexpected layout shift that occurs during the entire lifespan of the page. A layout shift occurs any time a visible element changes its position from one rendered frame to the next.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What TTI measures
&lt;/h2&gt;

&lt;p&gt;TTI measures how long it takes a page to become fully interactive. A page is considered fully interactive when:&lt;/p&gt;

&lt;p&gt;The page displays useful content, which is measured by the First Contentful Paint,&lt;br&gt;
Event handlers are registered for most visible page elements, and&lt;br&gt;
The page responds to user interactions within 50 milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Lighthouse TTT evaluating criteria&lt;/em&gt;&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%2Fhw73771c97c9cr73gy1m.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%2Fhw73771c97c9cr73gy1m.png" alt="alt text" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for Bearing,&lt;br&gt;
I will be writing articles talking about react hooks and and web performance upcoming articles please follow to stay connected.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
    </item>
    <item>
      <title>Debouncing, and web performance</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Mon, 22 Mar 2021 07:13:34 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/debouncing-and-web-performance-2dfc</link>
      <guid>https://dev.to/amankumarsingh01/debouncing-and-web-performance-2dfc</guid>
      <description>&lt;p&gt;Debouncing is a important when you are making a scalable web product and going to JavaScript interview.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Debouncing, is an important practice which is used very often by web developers to improve the performance of browser. &lt;/p&gt;

&lt;p&gt;Debounce methods do not execute when invoked. Instead, they wait for a predetermined time before executing. If the same method is called again, the previous is cancelled and the timer restarts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Consider the example in this the function associated to the button will be called 2 sec after the button is pressed. No matter how many times you press is continuously it will be executed once. &lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/amansingh1020/embed/RwKbRBz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let us understand the code&lt;/strong&gt;&lt;br&gt;
&lt;/p&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;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;immediate&lt;/span&gt;&lt;span class="p"&gt;)&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;timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;executedFunction&lt;/span&gt;&lt;span class="p"&gt;()&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;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&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;later&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;immediate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&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;callNow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;immediate&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;later&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wait&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;callNow&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the debounce function takes three argument &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The function which needs to be executed.&lt;/li&gt;
&lt;li&gt;The wait time.&lt;/li&gt;
&lt;li&gt;do you want to invoke this immediately (optional).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  we can also implement this in react very easily with help of &lt;code&gt;lodash&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Consider someone is building a search application making an api call after every key stroke. consider this example with help of debouncing you can visualize the difference between events fired.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amansingh1020/embed/BapBzQB?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's understand the code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;onSearchItemWithDebounce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;queryWithDebounce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//Do Stuff after state is updated.&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="s1"&gt;search with Debounce: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queryWithDebounce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;eventsFiredDebounce&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eventsFiredDebounce&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="nx"&gt;WAIT_TIME&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;**Note : debounce is imported from lodash**&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Thanks for Bearing,&lt;br&gt;
I will be writing articles explaining each hook provided by react in upcoming articles please follow to stay connected.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>useState hook simplified!</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Sat, 06 Mar 2021 11:36:48 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/setstate-hook-simplified-24mp</link>
      <guid>https://dev.to/amankumarsingh01/setstate-hook-simplified-24mp</guid>
      <description>&lt;h1&gt;
  
  
  Lets Begin the discussion
&lt;/h1&gt;

&lt;p&gt;useState is a react hook, we call it inside a functional component to add some local state to it. Between re-renders React actually preserves the state.&lt;/p&gt;

&lt;p&gt;useState returns a pair &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. current state&lt;/li&gt;
&lt;li&gt;2.  function that lets you update it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this is how you declare &lt;code&gt;const [ age, setAge ] = React.useState(10);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;consider this functional component, it updates the age.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setAge&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&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="c1"&gt;// Onclick function to update the state&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nf"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;age&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
             &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updateAge&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;   
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What happens here lets discuss
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Whenever this button is clicked updateAge function is called.&lt;/li&gt;
&lt;li&gt;The setAge method is called and state is updated.&lt;/li&gt;
&lt;li&gt;The Component renders.&lt;/li&gt;
&lt;li&gt;useState is called for updated state value. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This perception is very &lt;strong&gt;Wrong !!&lt;/strong&gt;, personally, I had the same misconception. To clear this and prove this point, I will ask you a question&lt;/p&gt;

&lt;h2&gt;
  
  
  Did it ever occur to you that useState hook doesn't update immediately ?
&lt;/h2&gt;

&lt;p&gt;The answer to this is because they don't make any direct change to the state object, instead they create queue to optimize performance. This is why they Do not reflect immediately.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;React.useState&lt;/code&gt; create queues for React core to update the state object of a React component.&lt;/p&gt;

&lt;p&gt;So the process to update React state is &lt;code&gt;asynchronous&lt;/code&gt; for performance reasons. That’s why changes don’t feel immediate.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;react.useState&lt;/code&gt; is asynchronous now how someone will perform something after after react state is changed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setAge&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&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="c1"&gt;// Onclick function to update the state&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nf"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;age&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;// This part will act as a callback whenever state updated &lt;/span&gt;
     &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;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;age&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="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age can't be less then 0, you dumb&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="p"&gt;},[&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
     &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
             &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updateAge&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
             &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;   
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Rules for using useState
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;1. Only call Hooks at the top level.&lt;/li&gt;
&lt;li&gt;2. Only call Hooks from React functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for Bearing,&lt;br&gt;
I will be writing articles explaining each hook provided by react in upcoming articles please follow to stay connected.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>An Introduction to react hooks rules</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Thu, 25 Feb 2021 17:14:20 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/an-introduction-to-react-hooks-rules-37m</link>
      <guid>https://dev.to/amankumarsingh01/an-introduction-to-react-hooks-rules-37m</guid>
      <description>&lt;p&gt;Through this article i will try to explain react hooks in beginner friendly manner hope you enjoy and learn through this article. &lt;/p&gt;

&lt;p&gt;Let's try to understand in what ways you can use hooks &lt;/p&gt;

&lt;h1&gt;
  
  
  Rules of Hooks
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Only use hook at the top level
&lt;/h1&gt;

&lt;p&gt;Never call hooks inside &lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1 .loops&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2 .conditions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;3 .nested functions&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;On contrary always use at the top level of your react function. This is necessary to ensure that Hooks are called in the same order each time a component renders. That’s what allows React to correctly preserve the state of Hooks between multiple &lt;strong&gt;useState and useEffect&lt;/strong&gt; calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Only hooks from react function
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;✅ Call Hooks from React function components.&lt;/li&gt;
&lt;li&gt;✅ Call Hooks from custom Hooks&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Now lets see some example of what we discussed above
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Form&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. Use the name state variable&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 2. Use an effect for persisting the form&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;persistForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;formData&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// 3. Use the surname state variable&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSurname&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Poppins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 4. Use an effect for updating the title&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateTitle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Order of execution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// 1. Initialize the name state variable with 'Mary'&lt;/span&gt;
&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistForm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// 2. Add an effect for persisting the form&lt;/span&gt;
&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Poppins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// 3. Initialize the surname state variable with 'Poppins'&lt;/span&gt;
&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateTitle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// 4. Add an effect for updating the title&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example of what we cannot do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// 🔴 We're breaking the first rule by using a Hook in a condition&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;name&lt;/span&gt; &lt;span class="o"&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;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;persistForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;formData&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// 1. Read the name state variable (argument is ignored)&lt;/span&gt;
&lt;span class="c1"&gt;// useEffect(persistForm)  // 🔴 This Hook was skipped!&lt;/span&gt;
&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Poppins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// 🔴 2 (but was 3). Fail to read the surname state variable&lt;/span&gt;
&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateTitle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// 🔴 3 (but was 4). Fail to replace the effect&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead this is what we can do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(function persistForm() {
    // 👍 We're not breaking the first rule anymore
    if (name !== '') {
      localStorage.setItem('formData', name);
    }
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I will be writing articles explaining each hooks  provided by react in upcoming articles please follow to stay connected.&lt;/p&gt;

&lt;p&gt;For more detailed explanation &lt;a href="https://reactjs.org/docs/hooks-rules.html" rel="noopener noreferrer"&gt;please visit&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>redux</category>
    </item>
    <item>
      <title>Some important HTML tags, one should know about</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Tue, 23 Feb 2021 06:15:17 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/some-important-html-tags-one-should-know-about-2og9</link>
      <guid>https://dev.to/amankumarsingh01/some-important-html-tags-one-should-know-about-2og9</guid>
      <description>&lt;p&gt;There are some html tags which is which is lesser talked but has potential to reduce the developer headache.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Lazy loading the image
&lt;/h1&gt;

&lt;p&gt;Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the length of the critical rendering path, which translates into reduced page load times.&lt;/p&gt;

&lt;p&gt;Lazy loading can occur on different moments in the application, but it typically happens on some user interactions such as scrolling and navigation. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;img src = "image.jpeg" alt = "Image" loading="lazy" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This also works like magic with iframe and is supported in most of the browsers.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Picture tag
&lt;/h1&gt;

&lt;p&gt;The HTML  element contains zero or more  elements and one &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element to offer alternative versions of an image for different display/device scenarios.&lt;/p&gt;

&lt;p&gt;The browser will consider each child  element and choose the best match among them. If no matches are found—or the browser doesn't support the  element—the URL of the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element's src attribute is selected. The selected image is then presented in the space occupied by the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;picture&amp;gt;
    &amp;lt;source srcset="image800px.png"
            media="(min-width: 800px)"&amp;gt;
    &amp;lt;img src="imagedefault.png" alt="" /&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is supported by every browser except Internet Explorer.&lt;/p&gt;
&lt;h1&gt;
  
  
  3 http-equiv= “refresh”
&lt;/h1&gt;

&lt;p&gt;I just want to discuss a usecase for this &lt;br&gt;
Suppose when a user first comes to your website, and you want to show him some loading screen for few seconds to pre-fetch content for a better user experience. In this case it is very useful.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;meta http-equiv="refresh" content="30; www.streamstyle.live"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will redirect to streamstyle after 30 sec&lt;/p&gt;

&lt;p&gt;Instructions for content attribute:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The number of seconds until the page should be reloaded - only if the &lt;strong&gt;content&lt;/strong&gt; attribute contains a positive integer.&lt;/p&gt;

&lt;p&gt;The number of seconds until the page should redirect to another - only if the &lt;strong&gt;content&lt;/strong&gt; attribute contains a positive integer followed by the string ';url=', and a valid URL.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  4. Progress
&lt;/h1&gt;

&lt;p&gt;The  tag represents the completion progress of a task.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;progress id="file" value="32" max="100"&amp;gt; 32% &amp;lt;/progress&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  5. Datalist
&lt;/h1&gt;

&lt;p&gt;The HTML  element contains a set of  elements that represent the permissible or recommended options available to choose from within other controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="ice-cream-choice"&amp;gt;Choose a flavor:&amp;lt;/label&amp;gt;
&amp;lt;input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" /&amp;gt;

&amp;lt;datalist id="ice-cream-flavors"&amp;gt;
    &amp;lt;option value="Chocolate"&amp;gt;
    &amp;lt;option value="Coconut"&amp;gt;
    &amp;lt;option value="Mint"&amp;gt;
    &amp;lt;option value="Strawberry"&amp;gt;
    &amp;lt;option value="Vanilla"&amp;gt;
&amp;lt;/datalist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I Think Html and css is very powerful, and these can be used to inject life in some good websites just by using these two. Despite of heavy usage in industry many people don't dive in deep.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading my article!&lt;/p&gt;

</description>
      <category>react</category>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Some important HTML tags, one should know about</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Tue, 23 Feb 2021 06:09:57 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/html-tags-nobody-is-talking-about-2d8i</link>
      <guid>https://dev.to/amankumarsingh01/html-tags-nobody-is-talking-about-2d8i</guid>
      <description>&lt;p&gt;There are some html tags which is which is lesser talked but has potential to reduce the developer headache.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Lazy loading the image
&lt;/h1&gt;

&lt;p&gt;Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the length of the critical rendering path, which translates into reduced page load times.&lt;/p&gt;

&lt;p&gt;Lazy loading can occur on different moments in the application, but it typically happens on some user interactions such as scrolling and navigation. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;img src = "image.jpeg" alt = "Image" loading="lazy" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This also works like magic with iframe and is supported in most of the browsers.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Picture tag
&lt;/h1&gt;

&lt;p&gt;The HTML  element contains zero or more  elements and one &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element to offer alternative versions of an image for different display/device scenarios.&lt;/p&gt;

&lt;p&gt;The browser will consider each child  element and choose the best match among them. If no matches are found—or the browser doesn't support the  element—the URL of the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element's src attribute is selected. The selected image is then presented in the space occupied by the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; element.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;picture&amp;gt;
    &amp;lt;source srcset="image800px.png"
            media="(min-width: 800px)"&amp;gt;
    &amp;lt;img src="imagedefault.png" alt="" /&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is supported by every browser except Internet Explorer.&lt;/p&gt;
&lt;h1&gt;
  
  
  3 http-equiv= “refresh”
&lt;/h1&gt;

&lt;p&gt;I just want to discuss a usecase for this &lt;br&gt;
Suppose when a user first comes to your website, and you want to show him some loading screen for few seconds to pre-fetch content for a better user experience. In this case it is very useful.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;meta http-equiv="refresh" content="30; https://amankumarsingh01.github.io"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will redirect to streamstyle after 30 sec&lt;/p&gt;

&lt;p&gt;Instructions for content attribute:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The number of seconds until the page should be reloaded - only if the &lt;strong&gt;content&lt;/strong&gt; attribute contains a positive integer.&lt;/p&gt;

&lt;p&gt;The number of seconds until the page should redirect to another - only if the &lt;strong&gt;content&lt;/strong&gt; attribute contains a positive integer followed by the string ';url=', and a valid URL.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  4. Progress
&lt;/h1&gt;

&lt;p&gt;The  tag represents the completion progress of a task.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;progress id="file" value="32" max="100"&amp;gt; 32% &amp;lt;/progress&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  5. Datalist
&lt;/h1&gt;

&lt;p&gt;The HTML  element contains a set of  elements that represent the permissible or recommended options available to choose from within other controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="ice-cream-choice"&amp;gt;Choose a flavor:&amp;lt;/label&amp;gt;
&amp;lt;input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" /&amp;gt;

&amp;lt;datalist id="ice-cream-flavors"&amp;gt;
    &amp;lt;option value="Chocolate"&amp;gt;
    &amp;lt;option value="Coconut"&amp;gt;
    &amp;lt;option value="Mint"&amp;gt;
    &amp;lt;option value="Strawberry"&amp;gt;
    &amp;lt;option value="Vanilla"&amp;gt;
&amp;lt;/datalist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I Think Html and css is very powerful, and these can be used to inject life in some good websites just by using these two. Despite of heavy usage in industry many people don't dive in deep.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading my article!&lt;/p&gt;

</description>
      <category>react</category>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Styled-components with typescript</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Mon, 22 Feb 2021 08:15:41 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/styled-components-with-typescript-5g7</link>
      <guid>https://dev.to/amankumarsingh01/styled-components-with-typescript-5g7</guid>
      <description>&lt;h1&gt;
  
  
  First lets discuss what is styled components and how it is useful :-
&lt;/h1&gt;

&lt;p&gt;styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users. &lt;/p&gt;

&lt;p&gt;The Motivation behind styled components were the following&lt;br&gt;
1.Automatic critical CSS&lt;br&gt;
2.No class name bugs&lt;br&gt;
3.Easier deletion of CSS&lt;br&gt;
4.Simple dynamic styling&lt;br&gt;
5.Painless maintenance&lt;br&gt;
6.Automatic vendor prefixing&lt;/p&gt;
&lt;h1&gt;
  
  
  How we can use styled-component with typescript
&lt;/h1&gt;

&lt;p&gt;consider this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  color: red;
  font-size : 21px;
  margin-left : 1.5rem;
  &amp;amp;::before {
    content: "&amp;lt;div&amp;gt;";
    font-family: "La Belle Aurore", cursive;
    color: yellow;
    font-size: 18px;
    position:absolute;

  }
  &amp;amp;::after {
    content: "&amp;lt;/div&amp;gt;";
    font-family: "La Belle Aurore", cursive;
    color: yellow;
    font-size: 18px;
    position:absolute;
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if you are not passing any props for dynamic styling then there is no difference.&lt;/p&gt;

&lt;p&gt;Lets Consider you are using something like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jsx &amp;lt;DIV backgroound ="red" color = "yellow" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;for using this type of component using styled components we need to have a interface for the props variables like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;StyledDivProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DIV&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledDivProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;`
  color: &lt;/span&gt;&lt;span class="p"&gt;${({&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
  background-color: &lt;/span&gt;&lt;span class="p"&gt;${({&lt;/span&gt; &lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
  font-size : &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;Constants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FONT_SIZE_H1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
  margin-left : 1.5rem;
  &amp;amp;::before {
    content: "&amp;lt;div&amp;gt;";
    font-family: "La Belle Aurore", cursive;
    color: green;
    font-size: 18px;
    position:absolute;

  }
  &amp;amp;::after {
    content: "&amp;lt;/div&amp;gt;";
    font-family: "La Belle Aurore", cursive;
    color: green;
    font-size: 18px;
    position:absolute;
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is how you can use styled-components with typescript.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>jsx</category>
    </item>
    <item>
      <title>JavaScript Scopes.</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Mon, 07 Sep 2020 17:39:56 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/javascript-scopes-l0j</link>
      <guid>https://dev.to/amankumarsingh01/javascript-scopes-l0j</guid>
      <description>&lt;h1&gt;
  
  
  What is Scope?
&lt;/h1&gt;

&lt;p&gt;A set of well defined rules for storing variables in any programming language is termed as scope.&lt;/p&gt;

&lt;p&gt;Now we know what is scope, take a pause and read the above line again, can you tell why do we need variables in programming language, we use variables everyday after writing hello world program, but why? Life was pretty simple without variables, why do we need this complexity?&lt;/p&gt;

&lt;p&gt;One of the most important paradigms of all programming languages is the ability of declaring variables, and later retrieve and modify those values, This ability gives a state to our program.  &lt;/p&gt;

&lt;p&gt;To understand scope, lets first see how compiler works.&lt;/p&gt;

&lt;h1&gt;
  
  
  Compiler Theory.
&lt;/h1&gt;

&lt;p&gt;JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.&lt;/p&gt;

&lt;p&gt;Consider this statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For running a JavaScript code these components play important role&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Engine - Responsible for start to finish compilation and execution&lt;/li&gt;
&lt;li&gt;Compiler- Handles the work of parsing and code generation.&lt;/li&gt;
&lt;li&gt;Scope - Maintains a lookup list of all declared identifiers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;var x =100;&lt;/code&gt; Compiler will perform lexing to breakdoen into tokens which will then parse into a tree.&lt;br&gt;
compiler will proceed as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encountering var x, compiler will ask scope to check if variables already exist in that particular scope, if so compiler ignores this declaration, Otherwise compiler will ask to declare a new variable called x for that scope.&lt;/li&gt;
&lt;li&gt;Compiler then generates code for engine, later execute the assignment x =100.&lt;/li&gt;
&lt;li&gt;The Engine runs the code generated by compiler and ask the scope if there is a variable x accessible in current scope, if  so engine uses the variable and assigns 100 to it. If not then engine will look into a scope above current scope. If engine eventually do not find any variable x then it will yell out an error!&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Knowing about engine
&lt;/h4&gt;

&lt;p&gt;While executing the code Engine has to lookup for x in the scope to see whether it is declared or not. In this case Engine will perform &lt;em&gt;LHS LOOKUP&lt;/em&gt; and when engine has to assign 100 to x it preforms &lt;em&gt;RHS LOOKUP&lt;/em&gt;&lt;br&gt;
for more clarity lets consider an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function PrintX(x){
console.log(x);
}
PrintX(100);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this code will we executed by engine the following steps will happen &lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Engine will tell scope that it has a RHS reference to PrintX , &lt;/li&gt;
&lt;li&gt;Scope will answer that exist in the scope and it is a function&lt;/li&gt;
&lt;li&gt;Engine will execute the function PrintX(), again Engine will ask compiler that it has a LHS reference for x with value 100.&lt;/li&gt;
&lt;li&gt;scope will say that compiler declared a formal parameter to the function PrintX.&lt;/li&gt;
&lt;li&gt;Engine will assign 100 to x.&lt;/li&gt;
&lt;li&gt;Engine will again ask scope that it has a RHS look  for X.&lt;/li&gt;
&lt;li&gt;Scope will say it has a variable x.&lt;/li&gt;
&lt;li&gt;Then the &lt;code&gt;console.log(x)&lt;/code&gt; will be executed and result &lt;code&gt;100&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Nested scope
&lt;/h2&gt;

&lt;p&gt;if scope is unable to find a variable/identifier in current scope then it moves upward a scope and checks there and this continues till it reaches the global scope and still if it didn't get that particular identifier it will yell an error!&lt;/p&gt;

&lt;p&gt;to visualize the process of nested scope resolution consider a multistorey building the ground floor is where the current scope lives now suppose the LHS/RHS lookup happens and the identifier is not found, taking the elevator to next floor and searching it there. The topmost floor is where the global state lives.&lt;/p&gt;

&lt;p&gt;if the engine is doing a LHS lookup and it didn't find the and arrives into global store it will end up creating a global scope variable. to avoid this we use "strict mode".&lt;/p&gt;

&lt;p&gt;Thanks for bearing! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Nodejs terminal app</title>
      <dc:creator>Aman Kumar Singh</dc:creator>
      <pubDate>Fri, 04 Sep 2020 04:16:51 +0000</pubDate>
      <link>https://dev.to/amankumarsingh01/nodejs-terminal-app-4nd6</link>
      <guid>https://dev.to/amankumarsingh01/nodejs-terminal-app-4nd6</guid>
      <description>&lt;p&gt;By the end of this post, you will learn to create your own commands for Linux to make your life simpler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets Begin
&lt;/h2&gt;

&lt;p&gt;First of all, you need node and npm installed in your system to install node and npm you can follow  &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;install node &amp;amp; npm&lt;/a&gt; download and install the latest stable version of node and npm.&lt;/p&gt;

&lt;h2&gt;
  
  
  * Creating the project
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir node-cli
$ cd node-cli
$ npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;hurray we created a node application 😃&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  step 1
&lt;/h2&gt;

&lt;p&gt;you can open the project in any code editor of choice mine is vs code.&lt;/p&gt;

&lt;p&gt;there will be a file called package.json that is used by NPM (Node Package Manager) uses this package.json file information about Node JS Application information or Node JS Package details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "node-cli",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your package.json will be looking like this after &lt;code&gt;npm init -y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;let's add a property &lt;code&gt;"bin":"./index.js"&lt;/code&gt; to this package.json file.&lt;/p&gt;

&lt;p&gt;Let us start doing the real thing now,&lt;br&gt;
make a file index.js and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.js
console.log("Hello, world!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now for testing purpose, we will do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index.js
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, it is working, great 😃,&lt;br&gt;
let's now focus on the real thing.&lt;/p&gt;

&lt;p&gt;in your index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node;
console.log("Hello, world!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;#!/usr/bin/env node this line should be the first line of your file basically when you execute this file it will tell that the system what interpreter to pass that file to for execution, via the command line following the magic #! prefix (called shebang).&lt;/p&gt;

&lt;p&gt;let's make this index.js file executable, in the terminal run 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;$ chmod +x index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it will make the file executable &lt;code&gt;./index.js&lt;/code&gt; to run your application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;let me tell you the capability of our command:&lt;br&gt;
It is capable of doing creating a react app and pushing to your Github account, to keep you synced. creating a new project and pushing it to Github is redundant, it's needed to be automated. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h6&gt;
  
  
  node modules we need for the project
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add minimist axios path
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the use of &lt;strong&gt;minimist&lt;/strong&gt; : the guts of optimist's argument parser without all the fanciful decoration.&lt;/p&gt;

&lt;p&gt;the use of &lt;strong&gt;axios&lt;/strong&gt; : it is used for passing the HTTP request.&lt;/p&gt;

&lt;p&gt;the use of &lt;strong&gt;path&lt;/strong&gt; : The path module provides utilities for working with file and directory paths.&lt;/p&gt;

&lt;p&gt;we will also use &lt;code&gt;child_process&lt;/code&gt; it comes prebuild with node.&lt;/p&gt;

&lt;h3&gt;
  
  
  use of minimist
&lt;/h3&gt;

&lt;p&gt;suppose you want to run our index.js file you do &lt;code&gt;./index.js&lt;/code&gt; but if you want to pass arguments to the program like &lt;code&gt;./index.js --file ./ --n first&lt;/code&gt;&lt;br&gt;
minimist will give you an object like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   file : './',
   n : 'first'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can use minimist in this way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var args = require("minimist")(process.argv.slice(2), {
  boolean: ["help", "check"],
  string: ["n", "path", "setup"],
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the type of help and check will be boolean and n, path, and setup will be of type string.&lt;/p&gt;

&lt;h3&gt;
  
  
  use of Axios
&lt;/h3&gt;

&lt;p&gt;Axios is used to make HTTP request, you can use Axios like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { default: Axios } = require("axios");
const payload = {
          name: `${args.n}`,
          description: "this is text",
          homepage: "https://github.com",
          private: false,
        };
        Axios.post("https://api.github.com/user/repos", payload, {
          headers: {
            "Content-Type": "application/json",
            Authorization: `token ${Key}`,
          },
        }).then(res =&amp;gt; console.log(res.data")).catch(e =&amp;gt; console.err(e));

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

&lt;/div&gt;



&lt;p&gt;Axios returns a promise that we check if the promise is fulfilled &lt;code&gt;.then()&lt;/code&gt; will we  called and if it fails &lt;code&gt;.catch()&lt;/code&gt; will be called &lt;/p&gt;

&lt;h3&gt;
  
  
  use of path
&lt;/h3&gt;

&lt;p&gt;The path module provides utilities for working with file and directory paths.&lt;/p&gt;

&lt;h3&gt;
  
  
  use of child_process
&lt;/h3&gt;

&lt;p&gt;The child_process module provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the child_process.spawn() function, but here we will mainly use exec() method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const exec = require("child_process").exec;
exec(` {your linux terminal commands or anything goes here }`,
     function (err, stdout, stderr) {
                if (err) {
                  console.error(`error: ${err.message}`);
                  return;
                }

                if (stderr) {
                  console.error(`stderr: ${stderr}`);
                }
                console.log("");
                if (stdout) {
                  console.error(`stdout: ${stdout}`);
                }
              }
            );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we know all about the packages we will be using.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content of index.js
&lt;/h2&gt;

&lt;p&gt;I guess it is understandable and makes sense!&lt;/p&gt;

&lt;p&gt;you can get your key here &lt;a href="https://github.com/settings/apps" rel="noopener noreferrer"&gt;create personal access token&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node
var path = require("path");
const exec = require("child_process").exec;
const { default: Axios } = require("axios");
const Key = &amp;lt;your_key /&amp;gt;
var args = require("minimist")(process.argv.slice(2), {
  boolean: ["help", "check"],
  string: ["n", "path"],
});

const BASEPATH = path.resolve(process.env.BASEPATH || __dirname);
if (args.help) {
  printHelp();
} else if (args.n) {
  if (args.path) {
    var pathto = path.join(BASEPATH, args.path);
    console.log("\x1b[32m", "work is in progress, please wait!");
    exec(
      `cd ${pathto} &amp;amp;&amp;amp; mkdir ${args.n} &amp;amp;&amp;amp; cd ${args.n} &amp;amp;&amp;amp; create-react-app ./`,
      (err, stdout, stderr) =&amp;gt; {
        if (err) {
          console.error(`error: ${err.message}`);
          return;
        }

        if (stderr) {
          console.error(`stderr: ${stderr}`);
          //return;
        }

        console.log("\x1b[32m", "Creating github repo!");

        const payload = {
          name: `${args.n}`,
          description: "this is text",
          homepage: "https://github.com",
          private: false,
        };
        Axios.post("https://api.github.com/user/repos", payload, {
          headers: {
            "Content-Type": "application/json",
            Authorization: `token ${Key}`,
          },
        })
          .then((res) =&amp;gt; {
            console.log(res.data);
            exec(
              `cd ${pathto}/${args.n} &amp;amp;&amp;amp; git init &amp;amp;&amp;amp; git remote add origin ${res.data.ssh_url} &amp;amp;&amp;amp; git add . &amp;amp;&amp;amp; git branch -M master &amp;amp;&amp;amp; git push -u origin master `,
              function (err, stdout, stderr) {
                if (err) {
                  console.error(`error: ${err.message}`);
                  return;
                }

                if (stderr) {
                  console.error(`stderr: ${stderr}`);
                }
                console.log("");
                console.log(`cd ${pathto}/${args.n}`);
                console.log("yarn start");
                console.log("Happy hacking");
              }
            );
          })
          .catch((e) =&amp;gt; console.log("NetWork Error", e));
      }
    );
  } else {
    printHelp();
  }
} else {
  printHelp();
}
//************************************************
function printHelp() {
  console.log("github usage:");
  console.log("");
  console.log(
    "This package can be used while creating a react app and at the same time get synced with github"
  );
  console.log("");
  console.log("--help                             Gives you help window");
  console.log(
    "--n ={fineName} --path ={path}                    File name of the project"
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's create a command-line function for this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm link
npm WARN react-app@1.0.0 No description
npm WARN react-app@1.0.0 No repository field.

audited 35 packages in 0.769s

3 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

/usr/local/bin/react-app -&amp;gt; /usr/local/lib/node_modules/react-app/index.js
/usr/local/lib/node_modules/react-app -&amp;gt; /home/aman/Github
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you are all done.😃&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/AmanKumarSingh01/react-app-cli" rel="noopener noreferrer"&gt;Link to the repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>ubuntu</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
