<?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: Anand Kumar</title>
    <description>The latest articles on DEV Community by Anand Kumar (@elanandkumar).</description>
    <link>https://dev.to/elanandkumar</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%2F125730%2F0dbb2641-d1b6-4c74-a910-bcbcbfa1a11b.png</url>
      <title>DEV Community: Anand Kumar</title>
      <link>https://dev.to/elanandkumar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elanandkumar"/>
    <language>en</language>
    <item>
      <title>Create your own Deno module + Github Action</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sun, 31 May 2020 11:22:35 +0000</pubDate>
      <link>https://dev.to/elanandkumar/create-your-own-deno-module-github-action-44ab</link>
      <guid>https://dev.to/elanandkumar/create-your-own-deno-module-github-action-44ab</guid>
      <description>&lt;p&gt;In my last article, I mentioned my first hands-on experience on &lt;code&gt;Deno&lt;/code&gt;. Along with that, I have demonstrated how to create REST API by using &lt;code&gt;Deno&lt;/code&gt; with &lt;code&gt;oak&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you have missed it, here is the link to that article.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/elanandkumar" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F125730%2F0dbb2641-d1b6-4c74-a910-bcbcbfa1a11b.png" alt="elanandkumar"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/elanandkumar/deno-is-here-have-you-explored-it-yet-3alo" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Deno is here. Todo App(React) + Deno Server (API) + Docker.&lt;/h2&gt;
      &lt;h3&gt;Anand Kumar ・ May 17 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#deno&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#typescript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;This time, I was trying to explore creating own module in &lt;code&gt;Deno&lt;/code&gt;. So, I'll try to explain how we can do it here and will provide the source code too.&lt;/p&gt;

&lt;p&gt;Believe me, it is quite simple and there is no complexity in using it too.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;p&gt;As we all know that we can import any module from URL directly in Deno, so let's first create one file which we can run with Deno.&lt;/p&gt;

&lt;p&gt;As a best practice, we should name our main module as &lt;code&gt;mod.ts&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;mod.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getHelloMsg&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &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="s2"&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;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="nf"&gt;getHelloMsg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deno&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the above file with Deno using the below command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run mod.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the terminal will show the following output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Hello Deno
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to convert it to a module that others can use, let's slightly modify the &lt;code&gt;mod.ts&lt;/code&gt;. The updated code snippet is given below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getHelloMsg&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &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="s2"&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;I have added the &lt;code&gt;export&lt;/code&gt; keyword on line 1 before the function so that we can import it.&lt;/p&gt;

&lt;p&gt;Now, let's create one file where we can test it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;index.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getHelloMsg&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;./mod.ts&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="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getHelloMsg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;deno&lt;/span&gt;&lt;span class="dl"&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, run it using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output will be similar to what we have seen before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Hello Deno
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But isn't it something that we do anyway? Yes, we do.&lt;/p&gt;

&lt;p&gt;But when we push the &lt;code&gt;mod.ts&lt;/code&gt; file in our GitHub, we can use it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getHelloMsg&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="s2"&gt;https://raw.githubusercontent.com/YOUR_GIT/REPO_NAME/master/mod.ts&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="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getHelloMsg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deno&lt;/span&gt;&lt;span class="dl"&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Please note the first line which is highlighted above.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see we are importing the module directly from the URL which is kind of cool.&lt;/p&gt;

&lt;p&gt;We do not need any registry or any setup to publish it so that others can use it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The main highlight here is that now, we can put this module on GitHub and we can use it from the URL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, let's set up our action file for GitHub. Follow the steps given below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a directory &lt;code&gt;.github&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create another directory inside &lt;code&gt;.github&lt;/code&gt; named &lt;code&gt;workflows&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;workflows&lt;/code&gt; create a file called &lt;code&gt;deno.yml&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the sample file&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;.github/workflows/deno.yml&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;matrix&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;deno&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;1.0.0&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Test run with Deno ${{ matrix.deno }}&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@master&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Setup Deno module&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;denolib/setup-deno@master&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;deno-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{matrix.deno}}&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Print deno version&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deno --version&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Check format&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deno fmt --check&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run tests&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deno test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A couple of things are happening here but the important one is to note are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;on&lt;/code&gt;: This tells when the action to be triggered. In our case, it will be triggered on &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pull_request&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deno fmt&lt;/code&gt;: This one ensures that the files are formatted correctly which means badly formatted code will fail the action&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deno test&lt;/code&gt;: This triggers the Unit tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, these steps can ensure that module is in healthy condition till the action run on GitHub is green.&lt;/p&gt;

&lt;p&gt;This way, whenever we will push any changes/updates to our module, the actions will trigger the build(&lt;em&gt;according to the action setup&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;A complete example with usage and action is available on my GitHub URL. Feel free to use it to create/scaffold your module for deno.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/elanandkumar/deno-module-starter-kit" rel="noopener noreferrer"&gt;Deno module starter kit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please follow the &lt;code&gt;README.md&lt;/code&gt; on how to use it.&lt;/p&gt;

&lt;p&gt;In a future article, I'll come up with how to publish your module to &lt;code&gt;deno.land&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Till then.....&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>deno</category>
      <category>tutorial</category>
      <category>githubaction</category>
      <category>denomodule</category>
    </item>
    <item>
      <title>Deno is here. Todo App(React) + Deno Server (API) + Docker.</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sun, 17 May 2020 16:32:51 +0000</pubDate>
      <link>https://dev.to/elanandkumar/deno-is-here-have-you-explored-it-yet-3alo</link>
      <guid>https://dev.to/elanandkumar/deno-is-here-have-you-explored-it-yet-3alo</guid>
      <description>&lt;p&gt;Hello Learners. I am sure you all are staying home and staying safe. Tough time but good opportunity to learn a lot. Be it due to different work style, life style or just getting more time due to cut down on commuting to work premises.&lt;/p&gt;

&lt;p&gt;So, as we all heard that &lt;code&gt;Deno&lt;/code&gt; is coming out and is created by the same guy who created much popular &lt;code&gt;Node&lt;/code&gt;, &lt;strong&gt;Ryan Dahl&lt;/strong&gt;. I was excited too and finally &lt;code&gt;1.0.0&lt;/code&gt; came out.&lt;br&gt;
And I started to have some hands on with it. Due to that very reason, I am writing this blog to document my experience.&lt;/p&gt;

&lt;p&gt;I am not going to detail out on how to install &lt;code&gt;deno&lt;/code&gt; or how to use the modules in details. They are available on official documentation page.&lt;/p&gt;

&lt;p&gt;These are good to start with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Manual&lt;/em&gt;: &lt;a href="https://deno.land/manual"&gt;https://deno.land/manual&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;v1.0.0&lt;/em&gt;: &lt;a href="https://deno.land/v1"&gt;https://deno.land/v1&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, I'll mention what I tried, what I liked about it and what I am planning to explore further. But, as technologies keeps evolving, these lists are not like finite.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is Deno?
&lt;/h1&gt;

&lt;p&gt;As per official documentation, &lt;strong&gt;Deno&lt;/strong&gt; is a &lt;code&gt;secure&lt;/code&gt; runtime for &lt;code&gt;JavaScript&lt;/code&gt; and &lt;code&gt;TypeScript&lt;/code&gt;.&lt;br&gt;
It is like node but there are lot of enhancements done with it.&lt;br&gt;
To summarize few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has TypeScript at its core&lt;/li&gt;
&lt;li&gt;Features of Modern JavaScript&lt;/li&gt;
&lt;li&gt;No package manager&lt;/li&gt;
&lt;li&gt;Has first-class &lt;code&gt;await&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Secure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am pretty excited to get into it. Are you?&lt;/p&gt;
&lt;h1&gt;
  
  
  Why Deno?
&lt;/h1&gt;

&lt;p&gt;It was announced almost 2 years ago and Ryan Dahl mentioned some of the regrets he had with some early decisions in Node.&lt;/p&gt;

&lt;p&gt;Here is the video for reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=M3BM9TB-8yA"&gt;https://www.youtube.com/watch?v=M3BM9TB-8yA&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;Deno&lt;/code&gt; is like second iteration of a server side apps powered by JavaScript.&lt;/p&gt;

&lt;p&gt;Now, it is &lt;code&gt;v1.0.0&lt;/code&gt; and officially stable, we can safely start using it as there won't be any breaking changes till we get to &lt;code&gt;v2.0.0&lt;/code&gt;. :)&lt;/p&gt;
&lt;h1&gt;
  
  
  What I built?
&lt;/h1&gt;

&lt;p&gt;Okay, so here are the points that I wanted to cover/explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server:&lt;/strong&gt; REST Api server built on &lt;code&gt;Deno&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client:&lt;/strong&gt; A client application to consume those API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker:&lt;/strong&gt; Dockerise the complete working application&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Server:
&lt;/h2&gt;

&lt;p&gt;I wanted to have a very basic REST API which can be used for CRUD operation. So, here is what I have decided to create for API:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Route&lt;/th&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Summary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/todos&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;List all todos&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/todos/:id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;Get one todo by Id&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/todos&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;Create a todo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/todos/:id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;Update a todo by Id&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/todos:/id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Delete a todo by Id&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Looks familiar from Node and express perspective, right?&lt;/p&gt;

&lt;p&gt;The first thing, I wanted to have is something similar to &lt;code&gt;express&lt;/code&gt; or &lt;code&gt;koa&lt;/code&gt; that we use in &lt;code&gt;node&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After some look around and documentation, I found something similar to &lt;code&gt;koa&lt;/code&gt; for Deno and it is called &lt;code&gt;oak&lt;/code&gt;. Below is the link to the module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://deno.land/x/oak/"&gt;https://deno.land/x/oak/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the basic server setup to get started:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;index.ts&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Application&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="s2"&gt;./deps.ts&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;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PORT&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="s2"&gt;./config.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Instantiate the application&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="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Application&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;listenOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Listening on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;...`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// start the server&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listenOptions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;deps.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;send&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="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="s2"&gt;https://deno.land/x/oak/mod.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;v4&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="s2"&gt;https://deno.land/std/uuid/mod.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After basic server setup, here is how I have created the routes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;routing.ts&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getTodos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getTodo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addTodo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updateTodo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/todos/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteTodo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have imported this into &lt;code&gt;index.ts&lt;/code&gt; and then added the route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./routing.ts&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routes&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;At this point, I was able to complete and test my REST APIs and all were working fine.&lt;/p&gt;

&lt;p&gt;Now, to serve the client page, I have added one more route into &lt;code&gt;index.ts&lt;/code&gt; after &lt;code&gt;app.use(router.routes())&lt;/code&gt; line as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&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;Context&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;await&lt;/span&gt; &lt;span class="nx"&gt;send&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;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;Deno&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cwd&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;/public`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;index.html&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above route/code helped me to server my react app's &lt;code&gt;index.html&lt;/code&gt; file from &lt;code&gt;public folder&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Running the Deno server
&lt;/h4&gt;

&lt;p&gt;So, to run the server, we use &lt;code&gt;deno run ENTRY_POINT_FILE&lt;/code&gt;. As we are ready to run the application, let me explain security part a bit.&lt;br&gt;
In node, we don't specify whether the application can access the &lt;code&gt;environment&lt;/code&gt; variables or can read/write file system or can access the network.&lt;/p&gt;

&lt;p&gt;In Deno, these accesses are not provided by default due to sand-boxing. If required, we have to pass the arguments to our &lt;code&gt;deno run&lt;/code&gt; command. The application that I worked on, below is the command to make it work properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run &lt;span class="nt"&gt;--allow-env&lt;/span&gt; &lt;span class="nt"&gt;--allow-net&lt;/span&gt; &lt;span class="nt"&gt;--allow-read&lt;/span&gt; &lt;span class="nt"&gt;--allow-write&lt;/span&gt; index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see following arguments flag:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--allow-env&lt;/code&gt;: To read environment variables in our codebase&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--allow-net&lt;/code&gt;: Helpful for networking&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--allow-read&lt;/code&gt;: Allows to read filesystem&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--allow-write&lt;/code&gt;: Allows to write to filesystem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More can be read on official documentation page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Good to know
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can create a &lt;code&gt;dependency.ts&lt;/code&gt; [deps.ts] to have all third-party modules at one place. This helps us to manage dependencies better. Any alternate way? Thoughts?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;index.ts&lt;/code&gt;, you can observe that the last line is using &lt;code&gt;await&lt;/code&gt; without &lt;code&gt;async&lt;/code&gt;. This is one of the feature of Deno where it has first-class &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moreover, created some other files and introduced some middleware as per the &lt;code&gt;oak&lt;/code&gt; setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I have used simple &lt;code&gt;json&lt;/code&gt; file to persist the data for REST API, so no Database. ;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Client
&lt;/h2&gt;

&lt;p&gt;Nothing much here to be honest. I used &lt;code&gt;create-react-app&lt;/code&gt; to scaffold a basic react application but in TypeScript. Used redux for statemanagement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Good to know:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;My deno application server was configured on different port than the client application. So, to make the api call, proxy was required. This can be easily achieved with &lt;code&gt;create-react-app&lt;/code&gt; by doing a &lt;code&gt;proxy&lt;/code&gt; entry into the &lt;code&gt;package.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"proxy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:4000"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;blockquote&gt;
&lt;p&gt;NOTE: This is for development purpose only&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nothing fancy for client application and so I have used manual process to copy the build output into server's public folder. It wasn't the primary goal.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How client app looks?
&lt;/h4&gt;

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

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;To dockerise the app, I have created two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dockerfile&lt;/li&gt;
&lt;li&gt;docker-compose.yml&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For, dockerfile, I have used &lt;code&gt;hayd/deno:alpine-1.0.0&lt;/code&gt; image. This worked out of the box. Here is the sample file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; hayd/deno:alpine-1.0.0&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 4000&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; deno&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; deps.ts .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;deno cache deps.ts

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;deno cache index.ts

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["run", "--allow-net", "--allow-env", "--allow-read", "--allow-write", "index.ts"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above file's &lt;code&gt;CMD&lt;/code&gt; might look bit strange but as explained above, we have to pass these arguments depending on the nature of application and sandbox feature of &lt;code&gt;Deno&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And the &lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.7"&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;deno&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deno-todo:0.0.1"&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;4000:4000"&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;deno&lt;/span&gt;

&lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;deno&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I liked about Deno
&lt;/h2&gt;

&lt;p&gt;As we have seen how things progressed while I was trying out deno, here is what I liked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No package manager&lt;/li&gt;
&lt;li&gt;Once module is cached, the application starts really quick&lt;/li&gt;
&lt;li&gt;Sandboxing for better security&lt;/li&gt;
&lt;li&gt;ES6 support out of the box&lt;/li&gt;
&lt;li&gt;TypeScript&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Yet to explore
&lt;/h2&gt;

&lt;p&gt;Okay, so as I mentioned, this is my first hands on with &lt;code&gt;Deno&lt;/code&gt;  and there are lot to be explored but here are few things that I want to go into deeper:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What would be the best way to manage the versioning of the third party modules.&lt;/li&gt;
&lt;li&gt;Server side rendering&lt;/li&gt;
&lt;li&gt;Hot reloading of deno server&lt;/li&gt;
&lt;li&gt;Creating own module for deno&lt;/li&gt;
&lt;li&gt;More on middlewares&lt;/li&gt;
&lt;li&gt;Creating own middlewares&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Using github action for the module and make it available on github to be used by others&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;and many more....&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There would be lot more to learn and explore but this is a starting point for me to take a next step. I am hoping that his will give you all some insight.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In short, I liked Deno so far and this is something new and refreshing which keeps human being more motivated. I will try to keep up with it and will come up with more articles as time permits.&lt;/p&gt;

&lt;h4&gt;
  
  
  Source code is available on github.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/elanandkumar/deno-todo-app"&gt;https://github.com/elanandkumar/deno-todo-app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, Stay Home, Stay Safe and Keep Learning.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>deno</category>
      <category>react</category>
      <category>tutorial</category>
      <category>typescript</category>
    </item>
    <item>
      <title>What editor and font you use for WebDev</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sun, 10 May 2020 12:48:22 +0000</pubDate>
      <link>https://dev.to/elanandkumar/what-editor-and-font-you-use-for-webdev-4ae3</link>
      <guid>https://dev.to/elanandkumar/what-editor-and-font-you-use-for-webdev-4ae3</guid>
      <description>&lt;p&gt;Hello Folks,&lt;/p&gt;

&lt;p&gt;Need two suggestions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Editor: which one to use for web development?

&lt;ul&gt;
&lt;li&gt;Sublime&lt;/li&gt;
&lt;li&gt;VSCode&lt;/li&gt;
&lt;li&gt;Webstorm&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Font: Please suggest best font for above editor?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>A journey of creating first flutter app</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sun, 19 Apr 2020 17:04:49 +0000</pubDate>
      <link>https://dev.to/elanandkumar/a-journey-of-creating-first-flutter-app-48hd</link>
      <guid>https://dev.to/elanandkumar/a-journey-of-creating-first-flutter-app-48hd</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Update: New version published for the app. &lt;a href="https://github.com/elanandkumar/covid19info-IN/releases/download/v1.1.0/app-release-1.1.0.apk"&gt;Covid19Info-IN-1.1.0&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First thing first, I finally decided to do something different than React development or Javascript. So, I thought to give &lt;code&gt;Flutter&lt;/code&gt; a go.&lt;br&gt;
And, to be honest the journey so far went really well.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Special mention: &lt;code&gt;@PublicisSapient&lt;/code&gt; to help with providing Udemy license.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While following along the course, I have learnt few things in both simpler and harder way. After couple of weeks, I have completed my Flutter course and thought to give it go by creating something without referencing the tutorial.&lt;/p&gt;

&lt;p&gt;And then this &lt;code&gt;Covid19&lt;/code&gt; came which shook the whole world.&lt;/p&gt;

&lt;p&gt;While writing this blog, I am locked down in my home and I am doing Work from home for almost a month now. Things have changed in lot of ways like working, communicating etc.&lt;/p&gt;

&lt;p&gt;Nevertheless, coming to the point. I started to think on what I should build?&lt;br&gt;
While following up news and all, I came across &lt;code&gt;covid19indiaorg&lt;/code&gt;, an open-source react app and API tool for getting the latest data on Covid19 for India. Do check their github repository. &lt;a href="https://github.com/covid19india"&gt;https://github.com/covid19india&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I decided to create an app which can leverage their API to show the data.&lt;/p&gt;

&lt;p&gt;While doing the development, I had to follow couple of blogs, searching for charts plugin or sometimes to find the usage for a particular widget. In flutter world, widget is like main building block and can also be called as component.&lt;/p&gt;

&lt;p&gt;But, overall, the learning experience while build this app  were quite good and engaging. I know that the app is not 100% complete and need to have lot of code improvements/best practices etc. But I was able to run it successfully on real device including iOS and android both.&lt;/p&gt;

&lt;p&gt;So, here I am going to mention what features it has and the list of plugins I have used to achieve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Summary of the cases:

&lt;ul&gt;
&lt;li&gt;Confirmed&lt;/li&gt;
&lt;li&gt;Active&lt;/li&gt;
&lt;li&gt;Recovered&lt;/li&gt;
&lt;li&gt;Deceased
&amp;gt; &lt;em&gt;With beautiful graph visualization&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Tabular state wise data&lt;/li&gt;
&lt;li&gt;Pull to refresh on table to update main screen data&lt;/li&gt;
&lt;li&gt;A page with helpful links which can be navigated to find out more information&lt;/li&gt;
&lt;li&gt;A page to have 15 recent alerts/new feed&lt;/li&gt;
&lt;li&gt;And an FAQ section&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To achieve above, I have used following extra plugins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fl_chart&lt;/code&gt;: A plugin to create beautiful charts&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;modal_progress_hud&lt;/code&gt;: A plugin to show loader for async calls&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http&lt;/code&gt;: To make API call&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;intl&lt;/code&gt;: Used for formatting. Eg. Number&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;url_launcher&lt;/code&gt;: A plugin which provides feature handle external urls opening&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;time_formatter&lt;/code&gt;: To get natural language time format from milliseconds timestamp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There were development challenges that I faced but I would like to mention one major challenge that I had.&lt;/p&gt;

&lt;h4&gt;
  
  
  First challenge
&lt;/h4&gt;

&lt;p&gt;While I was trying to deploy to real device, I got an error where the build was failing. Somehow either flutter or cocoapod is not able to clear the &lt;code&gt;App.framework&lt;/code&gt; for iOS device when trying to change the device from simulator to the real device.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FIX:&lt;/em&gt; I had to remove the folder using &lt;code&gt;rm -rf ios/Flutter/App.framework&lt;/code&gt;. After that, I ran &lt;code&gt;flutter clean&lt;/code&gt; and did the build. It worked.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Though, same issue occurred while switching from real to the simulator device too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In case, if it still doesn't work, I am happy to support.&lt;/p&gt;

&lt;h4&gt;
  
  
  Second challenge:
&lt;/h4&gt;

&lt;p&gt;When I was changing the bundle identifier for iOS app, I thought to change these for android too. All went well but after a cleanup, when I tried to do the build, android build started failing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FIX:&lt;/em&gt; It was failing because after change in bundle identifier, somehow, the trace of existing one named &lt;code&gt;example&lt;/code&gt; remained in &lt;code&gt;android/app/src/main/kotlin/com/&lt;/code&gt; folder. Removing that example folder from this location fixed the issue.&lt;/p&gt;

&lt;p&gt;So far so good and I was able to run the application on both android and iOS successfully.&lt;/p&gt;

&lt;p&gt;There are things that need to be done and couple of them are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;li&gt;Code refactoring/optimization&lt;/li&gt;
&lt;li&gt;Adaptive UI for different devices&lt;/li&gt;
&lt;li&gt;And more&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, I am quite happy to achieve this also and I am sure when we learn something new and create something interesting, it is always that joyful moment and a sense of great achievement.&lt;/p&gt;

&lt;p&gt;I'll conclude this post here with the link to github source code. Feel free to look at, fork it and submit the improvements if you want to.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/elanandkumar/covid19info-IN"&gt;https://github.com/elanandkumar/covid19info-IN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last but not the list, here is the link to &lt;code&gt;apk&lt;/code&gt; file if you have android and want to give it a try.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/elanandkumar/covid19info-IN/blob/master/docs/covid19infoin.apk?raw=true"&gt;Download Covid19Info-IN App (&lt;em&gt;android only&lt;/em&gt;)&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As mentioned above, there will be issues but it is always better to have something instead of having nothing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  What next?
&lt;/h4&gt;

&lt;p&gt;I will be exploring &lt;code&gt;MobX&lt;/code&gt; for flutter and React both as and when time permits.&lt;/p&gt;

&lt;h5&gt;
  
  
  For now, Stay home, Stay safe.
&lt;/h5&gt;

&lt;p&gt;Happy learning.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>android</category>
      <category>flutter</category>
      <category>opensource</category>
    </item>
    <item>
      <title>DEV.TO | Telegram  Posts</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Tue, 14 Jan 2020 12:58:37 +0000</pubDate>
      <link>https://dev.to/elanandkumar/dev-to-telegram-posts-3d0</link>
      <guid>https://dev.to/elanandkumar/dev-to-telegram-posts-3d0</guid>
      <description>&lt;p&gt;Today, I received one message on telegram channel of &lt;code&gt;dev.to&lt;/code&gt; with a link to operator mono font which links to &lt;code&gt;cufonfonts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I wonder if it is legitimate. I checked Operator mono website and they don't have the font free for personal use as far as I know.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;cufonfonts&lt;/code&gt; says, it is free for personal use. Now, following are my concerns/points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is it legitimate?&lt;/li&gt;
&lt;li&gt;If not, shall &lt;code&gt;dev.to&lt;/code&gt; post such message in the channel which will increase the piracy?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thoughts?&lt;/p&gt;

&lt;p&gt;Please leave your opinion in the comment section below.&lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>Fix: MacOS Catalina and CommandLineTools Issue</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Thu, 31 Oct 2019 19:55:13 +0000</pubDate>
      <link>https://dev.to/elanandkumar/fix-macos-catalina-and-commandlinetools-issue-2m13</link>
      <guid>https://dev.to/elanandkumar/fix-macos-catalina-and-commandlinetools-issue-2m13</guid>
      <description>&lt;p&gt;Recently, I have upgraded mac to 10.15.1 and I got a weird issue with node-gyp while working on my node project. It started failing when I was doing &lt;code&gt;npm install&lt;/code&gt;. The issue reported was regarding the &lt;code&gt;xcode-select&lt;/code&gt; CLT. We usually avoid installing full xcode and install the CLT (Command Line Tools) using &lt;code&gt;xcode-select --install&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Tried to run the command again but it did not work as CLT was already installed.&lt;/p&gt;

&lt;p&gt;I did struggle for sometime to fix the issue. Finally, I found the solution and this is what I did.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigated to the folder &lt;code&gt;/Library/Developer/&lt;/code&gt; using terminal.&lt;/li&gt;
&lt;li&gt;Removed the folder named &lt;code&gt;CommandLineTools&lt;/code&gt;. &lt;em&gt;Use &lt;code&gt;sudo&lt;/code&gt; if needed.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Ran the command &lt;code&gt;xcode-select --install&lt;/code&gt; which prompts for installing the command line toos. Once done, everything is back to normal for my node project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting it here serves two purpose. One, It may help someone else and so won't hamper the productivity. Two, If there is another way to solve this issue, we can share it here through comment to let others know.&lt;/p&gt;

&lt;p&gt;Have you faced this issue? If so, how you fixed it? Tell your experience and solution in comment section below.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image credit: Unsplash - @maxcodes&lt;/em&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>On demand &amp; colorful console log in NodeJS</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Wed, 02 Oct 2019 10:25:09 +0000</pubDate>
      <link>https://dev.to/elanandkumar/on-demand-colorful-console-log-in-nodejs-1929</link>
      <guid>https://dev.to/elanandkumar/on-demand-colorful-console-log-in-nodejs-1929</guid>
      <description>&lt;p&gt;We all know and use &lt;code&gt;console.log&lt;/code&gt; while working in JavaScript. Be it any framework (like react) or NodeJS. In case of NodeJS, we can see the console log output in our terminal. At some point of time we try to remove some or add some of the &lt;code&gt;console.log&lt;/code&gt; during development or while trying to deploy it.&lt;br&gt;
What if we can toggle the logging when needed?&lt;br&gt;
And, what if we can add some color also to distinguish between different log messages.&lt;/p&gt;

&lt;p&gt;To start with, let's create one simple &lt;code&gt;server.js&lt;/code&gt; file. It will look something like below:&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="c1"&gt;// Dependencies&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//create a server:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Write response&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello NodeJS!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//write a response to the client&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;//end the response&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Response ended.&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="c1"&gt;// start listening to the server&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Node server is listening on 8080 port.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I have added some extra console messages for explanation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, when we start the server using &lt;code&gt;node server.js&lt;/code&gt;, the terminal is going to output log message as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yyNFS121--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/f31753bfd8bcb942397364a82ffdaf55/7ec06/node-log-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yyNFS121--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/f31753bfd8bcb942397364a82ffdaf55/7ec06/node-log-1.png" alt="nodejs-log-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright!&lt;/p&gt;

&lt;p&gt;Now, let's modify the &lt;code&gt;server.js&lt;/code&gt; file to enable on-demand logging.&lt;/p&gt;

&lt;p&gt;First, modify the &lt;code&gt;server.js&lt;/code&gt;. Updated code is given below:&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="c1"&gt;// Dependencies&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&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="nx"&gt;util&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;util&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="nx"&gt;debugLog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;debuglog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//create a server:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;debugLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Write response&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello NodeJS!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//write a response to the client&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//end the response&lt;/span&gt;
  &lt;span class="nx"&gt;debugLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Response ended.&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="c1"&gt;// start listening to the server&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&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="nx"&gt;debugLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Node server is listening on 8080 port.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see couple of changes here. Let me try to explain it stepwise.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First of all, I have added one require statement at &lt;code&gt;Line 3&lt;/code&gt; to import &lt;code&gt;util&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In next line (&lt;code&gt;Line 4&lt;/code&gt;), I have created a reference to &lt;code&gt;debuglog&lt;/code&gt; method call done using &lt;code&gt;util&lt;/code&gt;. Important thing to note here is the parameter provided in function call: &lt;strong&gt;server&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Finally, we replace our &lt;code&gt;console.log&lt;/code&gt; statement with &lt;code&gt;debugLog&lt;/code&gt; variable created at &lt;code&gt;Line 4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once changes are done, if we run the server using &lt;code&gt;node server.js&lt;/code&gt; command, no console output will be shown.&lt;/p&gt;

&lt;p&gt;So, you must be thinking that how to show the logs then?&lt;/p&gt;

&lt;p&gt;Well, to see the log, we need to change our command a little. New command syntax would be &lt;code&gt;NODE_DEBUG=server node server.js&lt;/code&gt;. Refer the screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OfGwTaNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/233c667c0e18eec35d0032e3c94f57b2/7ec06/node-log-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OfGwTaNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/233c667c0e18eec35d0032e3c94f57b2/7ec06/node-log-2.png" alt="nodejs-log-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can observe couple of changes in the above image. Let's try to understand those.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;NODE_DEBUG=server&lt;/code&gt; -&amp;gt; When we set &lt;code&gt;NODE_DEBUG&lt;/code&gt; to &lt;code&gt;server&lt;/code&gt;, it logs only those messages that is related to the key we passed during the initialization of &lt;code&gt;util.debuglog('server')&lt;/code&gt;. This way, we can have different on-demand logging across file.&lt;/li&gt;
&lt;li&gt;While the output is coming, it also adds a keyword before. In the above case it added &lt;code&gt;SERVER 10072:&lt;/code&gt;. This helps us to understand that which part of application this log belongs to or the keyword we have given to. A nice and clean way to differentiate our log too. :)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Coming to the second part where I have mentioned to have colourful console. Usually, we use &lt;code&gt;console.log&lt;/code&gt; like this:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;Hello console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Going forward, try to use it in this way:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;x1b[36m%s&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;x1b[0m&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output will be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JA4bWFoB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/8eb83f88d64b8eb45dc3a3b00599b108/7ec06/node-log-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JA4bWFoB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/8eb83f88d64b8eb45dc3a3b00599b108/7ec06/node-log-3.png" alt="nodejs-log-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Isn't that cool?&lt;/p&gt;

&lt;p&gt;I know, a bit of explanation is needed. So, the first parameter in &lt;code&gt;console.log&lt;/code&gt; is instructing the terminal to change the text color. And &lt;code&gt;%s&lt;/code&gt; is the placeholder for our log message. Now, to change the color to different value, simply play around with the number after &lt;code&gt;[&lt;/code&gt; and before &lt;code&gt;m&lt;/code&gt;. In the above example, we can update &lt;code&gt;36&lt;/code&gt; to &lt;code&gt;31&lt;/code&gt; to get red variant.&lt;/p&gt;

&lt;p&gt;This works with the on-demand logging also (explained above). And we can get something like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--za-nxfd4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/5df147975a4ab3f90e09c2aa1b5fab17/7ec06/node-log-4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--za-nxfd4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.elanandkumar.com/static/5df147975a4ab3f90e09c2aa1b5fab17/7ec06/node-log-4.png" alt="nodejs-log-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks gorgeous, right.&lt;/p&gt;

&lt;p&gt;In similar fashion, we can change the background of the log message instead of text color. I am providing the values for reference here. Go ahead and try these.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example given above, replace the content from start till &lt;code&gt;m&lt;/code&gt; letter with any one of the above and check the output. Feel free to add comments below with some nice looking console messages here and with the code used.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

&lt;p&gt;Happy learning!&lt;/p&gt;

</description>
      <category>node</category>
      <category>console</category>
      <category>ondemand</category>
      <category>logging</category>
    </item>
    <item>
      <title>MacOS Catalina: Top 3 features I liked most</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Wed, 12 Jun 2019 15:06:35 +0000</pubDate>
      <link>https://dev.to/elanandkumar/macos-catalina-top-3-features-i-liked-most-3bid</link>
      <guid>https://dev.to/elanandkumar/macos-catalina-top-3-features-i-liked-most-3bid</guid>
      <description>&lt;p&gt;Hello Folks,&lt;/p&gt;

&lt;p&gt;I recently installed MacOS Catalina on my macbook pro. Pretty stable TBH.&lt;/p&gt;

&lt;p&gt;First thing I noticed is that now we have &lt;code&gt;zsh&lt;/code&gt; as default shell instead of &lt;code&gt;bash&lt;/code&gt;. I think it is great. And i am pretty excited about it in stable releasw. What is your thought on this?&lt;/p&gt;

&lt;p&gt;Next thing that i really liked about is &lt;code&gt;sidecar&lt;/code&gt; which can help to use our ipad as secondary display. I did not try it yet but it sounds awesome and i am eagerly waiting to give it a try. I will put down my experience in comment section later.&lt;/p&gt;

&lt;p&gt;Last but not the least, I liked the uodate came for photos app. We have various options and aling with that app is gettting intellugent to display our best pictures.&lt;/p&gt;

&lt;p&gt;What are the top 3 features you like most in MacOS Catalina?&lt;/p&gt;

&lt;p&gt;Note down your thoughts in comment section below.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>What should I learn next?</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Tue, 28 May 2019 16:14:46 +0000</pubDate>
      <link>https://dev.to/elanandkumar/what-should-i-learn-next-1mgo</link>
      <guid>https://dev.to/elanandkumar/what-should-i-learn-next-1mgo</guid>
      <description>&lt;p&gt;Hello to all__.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;code&gt;dev.to&lt;/code&gt; for providing such a nice platform and to the great community that we have.&lt;/p&gt;

&lt;p&gt;From past 3 years, i am working on React. Recently I wrote some of articles too.&lt;/p&gt;

&lt;p&gt;This one is more about the help I want from our community friends, that what should i pick next to learn except React? What are the suggestions, options i have?&lt;/p&gt;

&lt;p&gt;Please help/guide me with this.&lt;/p&gt;

&lt;p&gt;Thank you in advance.&lt;/p&gt;

&lt;p&gt;Happy learning!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>help</category>
    </item>
    <item>
      <title>React component lifecycle with Hook§</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sat, 25 May 2019 01:39:34 +0000</pubDate>
      <link>https://dev.to/elanandkumar/react-component-lifecycle-with-hook-6lo</link>
      <guid>https://dev.to/elanandkumar/react-component-lifecycle-with-hook-6lo</guid>
      <description>&lt;p&gt;In previous post, I have explained how to convert a class component into functional component so that we can leverage the hooks.&lt;br&gt;
Here is the link to article:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/elanandkumar" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F125730%2F0dbb2641-d1b6-4c74-a910-bcbcbfa1a11b.png" alt="elanandkumar"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/elanandkumar/react-hooks-migrate-class-component-to-hooks-3879" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;React Hooks: Migrate class component to functional and use hooks&lt;/h2&gt;
      &lt;h3&gt;Anand Kumar ・ May 12 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#reacthooks&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this post, I will help you to understand how to get the same output that we used to achieve using component lifecycles with react hooks.&lt;/p&gt;

&lt;p&gt;I'll be covering following lifecycle methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;componentWillMount&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidMount&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidUpdate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shouldComponentUpdate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentWillUnmount&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we start, let's get some understanding about following hooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt;
### useState
&lt;code&gt;useState&lt;/code&gt; returns a stateful value and a function to update it.
#### Syntax:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.elanandkumar.com%2Fstatic%2Ff6e859f954689b36c47b8bdd9cbdc1e6%2F5cad1%2FuseState-syntax.jpg" alt="useState syntax"&gt;
### useEffect
With functional component, we are not &lt;strong&gt;allowed&lt;/strong&gt; to have side effects like mutations, timers, mutations or any other side effects inside the main body of the component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, here we have &lt;code&gt;useEffect&lt;/code&gt; which runs &lt;strong&gt;after&lt;/strong&gt; the render happens. By default, effects run after every render but there are ways to control it which we will look into it later in the article.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.elanandkumar.com%2Fstatic%2Fa0e61e77cdc8d734938c1f31ba58d450%2F9ab67%2FuseEffect-syntax.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.elanandkumar.com%2Fstatic%2Fa0e61e77cdc8d734938c1f31ba58d450%2F9ab67%2FuseEffect-syntax.jpg" alt="useEffect syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more details on hooks or for any other hooks provided by react, look at the official documentation.&lt;/p&gt;

&lt;p&gt;So, let's start with the component lifecycle and try to understand the replacement of the same with hooks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt; Do not use hooks inside any code blocks. It must be at the top level hierarchy of the functional component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see the running example of a small application. This usage react class based component and lifecycle. At the end of the article, you can check the same application created using functional component and has the similar lifecycle execution.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/anand-kr/embed/yWzqZd?height=600&amp;amp;default-tab=js,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Let's begin.&lt;/p&gt;
&lt;h3&gt;
  
  
  componentWillMount
&lt;/h3&gt;

&lt;p&gt;When we convert any class component to functional component, we can write the code after defining our states (if any) using &lt;code&gt;useState&lt;/code&gt; or before the code reaches the render (jsx) part to achieve the &lt;code&gt;componentWillMount&lt;/code&gt; behaviour. As we know the code execution happens from top to bottom.&lt;/p&gt;
&lt;h3&gt;
  
  
  componentDidMount
&lt;/h3&gt;

&lt;p&gt;As explained before about &lt;code&gt;useEffect&lt;/code&gt;, we can pass the second optionalParameter to achieve the &lt;code&gt;componentDidMount&lt;/code&gt; behavior. Let's have a look at the following code snippet.&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;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="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;Robot: componentDidMount&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ...code goes here...&lt;/span&gt;
  &lt;span class="nf"&gt;fetchData&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;In the above code, we have blank array &lt;code&gt;[]&lt;/code&gt; as the second argument at &lt;strong&gt;line 5&lt;/strong&gt;. Why? 🤔&lt;/p&gt;

&lt;p&gt;Well, because the second argument when provided helps react to invoke the effect only if it detects a change in any of the entries provided. This array is dependencies for the function that we pass as first argument in &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this case, it is blank which means it is never going to detect any change and so it will invoke only once. Isn't that what &lt;code&gt;componentDidMount&lt;/code&gt; does? Invokes only once, right?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that if the second argument is not passed in &lt;code&gt;useEffect&lt;/code&gt;, it will execute always after render.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  componentDidUpdate
&lt;/h3&gt;

&lt;p&gt;Let's say, we need to invoke api call again if any props changes for the component, we do it in here once the component updates. We can achieve it using &lt;code&gt;useEffect&lt;/code&gt;. Though, this time, we need to pass an entry to the second argument.&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;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="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;Robot: componentWillUpdate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// code goes here&lt;/span&gt;
  &lt;span class="nf"&gt;fetchData&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, we have passed a prop &lt;code&gt;selectedRobotId&lt;/code&gt; in the array. So, whenever this prop is going to change, the effect will run and we get the updated component.&lt;/p&gt;

&lt;h3&gt;
  
  
  shouldComponentUpdate
&lt;/h3&gt;

&lt;p&gt;This one is a bit tricky but let me help you to understand this. As we all know, this lifecycle method helps us to control the rendering of the component by our self. Following is an example:&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="nf"&gt;shouldComponentUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nextState&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Robot: shouldComponentUpdate&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;nextProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="nx"&gt;nextState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loadedRobot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loadedRobot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="nx"&gt;nextState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&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;But, with functional component, we can not use react component lifecycle methods this way. So, how to achieve this same result?&lt;/p&gt;

&lt;p&gt;Well, with react, we have something called &lt;code&gt;React.memo&lt;/code&gt;. We can wrap out component with this as shown below:&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does this look right as an alternate to &lt;code&gt;shouldComponentUpdate&lt;/code&gt;. No.....&lt;/p&gt;

&lt;p&gt;Ok, so here is the thing. &lt;code&gt;React.memo&lt;/code&gt; accepts a second argument (a function) which provides the access to &lt;code&gt;nextProps&lt;/code&gt; and &lt;code&gt;prevProps&lt;/code&gt;. This can help us to achieve the same thing as &lt;code&gt;shouldComponentUpdate&lt;/code&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nextProps&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;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;Robot: shouldComponentUpdate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nextProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&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;blockquote&gt;
&lt;p&gt;There is one thing to note here. This works opposite of &lt;code&gt;shouldComponentUpdate&lt;/code&gt;. You return &lt;strong&gt;true&lt;/strong&gt; if you do not want to re-render. You return &lt;strong&gt;false&lt;/strong&gt; if you want it to render.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React handles this beautifully and so the second function is only needed if you do not want a re-render for any props change. Else, let React handle it for you. :)&lt;/p&gt;

&lt;h3&gt;
  
  
  componentWillUnmount
&lt;/h3&gt;

&lt;p&gt;Remember, in the explanation of &lt;code&gt;useEffect&lt;/code&gt;, I mentioned about an optional return function inside the &lt;code&gt;useEffect&lt;/code&gt;. This return function serves the purpose of cleanup.&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;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="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;Robot: componentDidUpdate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;//...code goes here&lt;/span&gt;
  &lt;span class="nf"&gt;fetchData&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="o"&gt;=&amp;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cleaning up...&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is similar to &lt;code&gt;componentDidUpdate&lt;/code&gt; code block except it has a return function now. So, whenever &lt;code&gt;props.selectedRobotId&lt;/code&gt; changes, this effect runs and the return function invokes on completion of effect where we can do cleanup.&lt;br&gt;
But, this is not what happens with &lt;code&gt;componentWillUnmount&lt;/code&gt;. Right?&lt;br&gt;
Ok, so to achieve the &lt;code&gt;componentWillUnmount&lt;/code&gt; we can add one more &lt;code&gt;useEffect&lt;/code&gt; in our code and pass the second argument as a blank array (&lt;code&gt;[]&lt;/code&gt;). Similar to the &lt;code&gt;componentDidMount&lt;/code&gt; but the &lt;code&gt;useEffect&lt;/code&gt; will have a return function this time inside, which will work as &lt;code&gt;componentWillUnmount&lt;/code&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;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="c1"&gt;//...code goes here&lt;/span&gt;
  &lt;span class="nf"&gt;fetchData&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="o"&gt;=&amp;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cleaning up...&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedRobotId&lt;/span&gt;&lt;span class="p"&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;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="c1"&gt;//...code goes here&lt;/span&gt;
  &lt;span class="nf"&gt;fetchData&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="o"&gt;=&amp;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Robot: componentWillUnmount&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="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives us the way to do clean up at two level. One, when component updates and one when it is ready to unmount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we covered &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;. Then we used &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;memo&lt;/code&gt; to achieve the component life cycle methods that we have with class based component.&lt;/p&gt;

&lt;p&gt;At first, this might look confusing but do not worry. Here is the codepen for same application (shown above) but using React hooks.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/anand-kr/embed/KLXGJQ?height=600&amp;amp;default-tab=js,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Please checkout the code and practice. Also, feel free to share it or reach out to me for any questions/concerns.&lt;/p&gt;

&lt;p&gt;Happy learning!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>advanced</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React Hooks: Migrate class component to functional and use hooks</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sun, 12 May 2019 09:02:47 +0000</pubDate>
      <link>https://dev.to/elanandkumar/react-hooks-migrate-class-component-to-hooks-3879</link>
      <guid>https://dev.to/elanandkumar/react-hooks-migrate-class-component-to-hooks-3879</guid>
      <description>&lt;p&gt;We all are aware that react hooks is ready for prime time and we started using it already. But, what if we have any existing project and we want to move to react hooks? Definitely, we have components which is written as class component.&lt;/p&gt;

&lt;p&gt;This post will guide you to change an existing class based component to react hook based component. &lt;em&gt;But, this post is not intended to go into the details of hooks.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is what react documentation says about &lt;code&gt;hooks&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks let us use state and other React features without writing a class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's begin with an example application. It is a simple counter application. The code is given below:&lt;/p&gt;



&lt;div class="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;initialData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maxLimit&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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;count&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialCount&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;onIncrement&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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;count&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;onDecrement&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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;count&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;render&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="si"&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;onDecrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&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;count&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="si"&gt;}&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="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="si"&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;count&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;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="si"&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;onIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&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;count&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&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="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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;CounterApp&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;initialData&lt;/span&gt; &lt;span class="p"&gt;}&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="nx"&gt;props&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&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;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Counter Example&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&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;Counter&lt;/span&gt;
          &lt;span class="na"&gt;initialCount=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;initialData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;maxLimit=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;initialData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;CounterApp&lt;/span&gt; &lt;span class="na"&gt;initialData=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;initialData&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'root'&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;As you can see, I have created a simple counter app. The class component that I have created are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CounterApp&lt;/code&gt;: This is the main app component&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Counter&lt;/code&gt;: The actual counter component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please note that, this code is intended for this post and for explanation purpose only. Please ignore any improvement areas that you may see. ☺&lt;/p&gt;

&lt;p&gt;So, to convert a class component to functional/hooks component, here are the steps:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pick the smallest class component first i.e. bottom to top approach would be better.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Move all of the code that a class component has into the render method and make it work. Eg. event handler functions or any other functions or code.&lt;/li&gt;
&lt;li&gt;Update the code to use object de-structuring for &lt;code&gt;props&lt;/code&gt; &amp;amp; &lt;code&gt;states&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once, &lt;strong&gt;#2&lt;/strong&gt; is complete, Add a line before the &lt;code&gt;class&lt;/code&gt; syntax like shown below.&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&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;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/*...code goes here */&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Move everything from render method to the updated function component code block.&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&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="c1"&gt;//...code inside render methods of class component goes here&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//...Rest of the code&lt;/span&gt;
  &lt;span class="nx"&gt;render&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 will be blank now&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;ol&gt;
&lt;li&gt;Move the de-structured code of props inside the paratenthis of functional component. Remove the code from inside the code block.&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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="cm"&gt;/*...rest of the code */&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Update the component to use hooks like &lt;code&gt;useState&lt;/code&gt; or any other hooks that might be needed. Eg.&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;//...Rest of the code&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;//...Rest of the code&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you have more that one state property, please change it to use hooks for all of them individually.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;I am not explaining the syntax for how to write hooks here. Please read official documentation for the same.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finally, update the return statement code block to use newly created props (de-structured) or the states using hooks. Or any other changes done, should be incorporated accordingly. And remove the class component line of code altogether.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Alright, that might look too much and not very clear. So let's work on the example that we have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counter Component
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Move the &lt;code&gt;onIncrement&lt;/code&gt; and &lt;code&gt;onDecrement&lt;/code&gt; code block inside the render method. Define it as &lt;code&gt;const&lt;/code&gt; and update the functions &lt;code&gt;this.onIncrement&lt;/code&gt; and &lt;code&gt;this.onDecrement&lt;/code&gt; to &lt;code&gt;onIncrement&lt;/code&gt; and &lt;code&gt;onDecrement&lt;/code&gt; respectively.&lt;/li&gt;
&lt;li&gt;Use object de-structuring for &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt; inside render method. In our example, you can do something like below:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&lt;/span&gt; &lt;span class="p"&gt;}&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="nx"&gt;props&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;count&lt;/span&gt; &lt;span class="p"&gt;}&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, just before the &lt;code&gt;class Counter extends React.Component {&lt;/code&gt; line, create a line similar to following:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Cut everything from &lt;code&gt;render&lt;/code&gt; method and paste it in between the curly brackets of the &lt;code&gt;const Counter = () =&amp;gt; {}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Move the de-structured &lt;code&gt;props&lt;/code&gt; into parenthesis. &lt;strong&gt;Line 2&lt;/strong&gt; is commented out and &lt;strong&gt;Line 1&lt;/strong&gt; has the props now &lt;code&gt;{maxLimit, initialCount}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;// const { maxLimit, initialCount } = this.props;&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;count&lt;/span&gt; &lt;span class="p"&gt;}&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....Rest of the code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Next, we need to change our &lt;code&gt;this.state&lt;/code&gt;. We will be using &lt;code&gt;useState&lt;/code&gt; hooks here. Update the code at &lt;strong&gt;Line 2&lt;/strong&gt; as shown below:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ....Rest of the code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code that we changed is syntax of using hooks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now is the time to update our function to use &lt;code&gt;setCount&lt;/code&gt; we defined using hooks. Change the code &lt;code&gt;this.setState&lt;/code&gt; that we have inside &lt;code&gt;onIncrement&lt;/code&gt; and &lt;code&gt;onDecrement&lt;/code&gt;. Updated code will look something like below:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;// ....Rest of the code&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onIncrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onIncrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;// ....Rest of the code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, update the code of return statement accordingly to use the new &lt;code&gt;count&lt;/code&gt; state (created using hooks) and de-structured &lt;code&gt;props&lt;/code&gt;. AND, remove the &lt;code&gt;class Counter extends React.Component {}&lt;/code&gt; code completely.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final code for &lt;code&gt;Counter&lt;/code&gt; component should look like:&lt;/p&gt;



&lt;div class="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;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialCount&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;onIncrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onDecrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onDecrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="si"&gt;}&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="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="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&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;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="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&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="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;p&gt;So, you must notice that at the end, there is no &lt;code&gt;this&lt;/code&gt; being used and code also looks lot more cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  CounterApp
&lt;/h2&gt;

&lt;p&gt;The main &lt;code&gt;CounterApp&lt;/code&gt; component doesn't have much in it and so we can convert it to functional component without hassle. The final code for &lt;code&gt;CounterApp&lt;/code&gt; will look like below.&lt;/p&gt;



&lt;div class="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;CounterApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;initialData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Counter Example&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&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;Counter&lt;/span&gt; &lt;span class="na"&gt;initialCount=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;maxLimit=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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;blockquote&gt;
&lt;p&gt;Wondering what &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt; is? This is an approach to avoid extra layer of elements that we used to created earlier. So, use this or use &lt;code&gt;React.Fragment&lt;/code&gt; as shown at the beginning of the article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As, we have refactored our code now, the complete counter app code is given below:&lt;/p&gt;



&lt;div class="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;initialData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maxLimit&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;// added style to make it look better, in case you want to see&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counterStyles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;maxWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"185px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;boxShadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5px 5px 4px #888888"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"flex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;alignItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"center"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;button&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"30px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;borderRadius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"50%"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;borderStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"none"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"22px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"khaki"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"30px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;paddingBottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pointer"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;countDiv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;minWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"80px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0 15px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;textAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"center"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"30px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lineHeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1px solid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;borderRadius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"10px"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0 5px"&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;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialCount&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;// styles...&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;container&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="nx"&gt;countDiv&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;counterStyles&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialCount&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;onIncrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onDecrement&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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="na"&gt;style=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="si"&gt;}&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;style=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onClick=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onDecrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;initialCount&lt;/span&gt;&lt;span class="si"&gt;}&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="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="na"&gt;style=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;countDiv&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;count&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;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;style=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onClick=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&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="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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CounterApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;initialData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxLimit&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counterStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Counter App: Hooks&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&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;Counter&lt;/span&gt; &lt;span class="na"&gt;initialCount=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;maxLimit=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;maxLimit&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;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;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;CounterApp&lt;/span&gt; &lt;span class="na"&gt;initialData=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;initialData&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'root'&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;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Through this post, we have learnt couple of tips on how to convert a class based component to functional and hook based component. I hope this will help you to migrate your existing code base.&lt;/p&gt;

&lt;p&gt;I have also recorded a video to accompany this article. Here is the link.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/OBPhmCHApF0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Feel free to subscribe to the channel and explore other videos too.&lt;/p&gt;

&lt;p&gt;In case of any questions/concerns, feel free to reach out.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>javascript</category>
    </item>
    <item>
      <title>ES6 and Beyond &gt; Part 2</title>
      <dc:creator>Anand Kumar</dc:creator>
      <pubDate>Sat, 04 May 2019 16:26:20 +0000</pubDate>
      <link>https://dev.to/elanandkumar/es6-and-beyond-part-2-p9m</link>
      <guid>https://dev.to/elanandkumar/es6-and-beyond-part-2-p9m</guid>
      <description>&lt;p&gt;This post is second part of the previous one. If you haven't read it yet, read it here:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/elanandkumar" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z-sSHUXw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--8IWlE7Fc--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://thepracticaldev.s3.amazonaws.com/uploads/user/profile_image/125730/0dbb2641-d1b6-4c74-a910-bcbcbfa1a11b.png" alt="elanandkumar image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/elanandkumar/es6-and-beyond-4f96" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;ES6 and Beyond &amp;amp;gt; Part 1&lt;/h2&gt;
      &lt;h3&gt;Anand Kumar ・ 6 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#es6&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#intermediate&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this article, we are going to learn about promises and the new &amp;amp; improved/alternate way to achieve asynchronous task using &lt;code&gt;async/await&lt;/code&gt; instead of promise.&lt;/p&gt;

&lt;p&gt;The objectives are to learn following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.prototype.finally&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;async/await&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;for...await...of&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Promise: An Introduction
&lt;/h2&gt;

&lt;p&gt;So, what is &lt;strong&gt;promise&lt;/strong&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is an object, which guarantees us to return a single value some time in future for an asynchronous operation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's understand it a bit more by understanding the states that a promise can have. A promise is an object (mentioned above) can have 3 states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;fulfilled:&lt;/em&gt; When promise is resolved/fulfilled.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;rejected:&lt;/em&gt; When a promise failed to get fulfilled.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;pending:&lt;/em&gt; Neither rejected not fulfilled is the pending state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now here is the syntax on how to write a promise in javascript:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;An explanation on above code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Line 2:&lt;/em&gt; We are creating/constructing a promise object. &lt;strong&gt;state&lt;/strong&gt; would be &lt;strong&gt;pending&lt;/strong&gt; and result will &lt;strong&gt;undefined&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Line 7:&lt;/em&gt; Promise is getting resolved here so the &lt;strong&gt;state&lt;/strong&gt; would be &lt;strong&gt;fulfilled&lt;/strong&gt; with a returned value of the string inside &lt;code&gt;resolve&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Line 10:&lt;/em&gt; Promise is getting rejected here. The &lt;strong&gt;state&lt;/strong&gt; would be &lt;strong&gt;rejected&lt;/strong&gt; and sets the result to error with the error value provided.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we can use the &lt;code&gt;promise&lt;/code&gt; object as shown below:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;So, promise provides &lt;code&gt;then&lt;/code&gt;-ability which is what guarantees us to get the data once a promise is resolved/rejected.&lt;/p&gt;

&lt;p&gt;Finally, we can derive some rules regards to &lt;code&gt;Promise&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A promise provides &lt;code&gt;then()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;A pending promise can result to either 'fulfilled' or 'rejected' state&lt;/li&gt;
&lt;li&gt;A fulfilled or rejected promise is considered as settled. So, it must not transition to any other state.&lt;/li&gt;
&lt;li&gt;Once a promise is settled, it should have a value which must not change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A bit more on &lt;code&gt;then&lt;/code&gt; is needed. Right? Ok, let's first look at the syntax of &lt;code&gt;then&lt;/code&gt;:&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onFulfilled&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;onRejected&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;onFulfilled&lt;/code&gt;: Callback for resolved/fulfilled promise&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onRejected&lt;/code&gt; (&lt;em&gt;optional&lt;/em&gt;): Callback for rejected promise&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;One of the great thing about promise is &lt;strong&gt;chaining&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To achieve the chaining, we use &lt;code&gt;then()&lt;/code&gt; method provided by promise. Let's have a look at following example.&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;promiseOne&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;promise2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onFulfilled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onRejected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;promiseOne&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onFulfilled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onRejected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So, each promise represents the completion of another asynchronous step in the chain.&lt;/p&gt;

&lt;p&gt;Before promise arrived, we fall in a &lt;strong&gt;classic callback pyramid of doom&lt;/strong&gt; trap. Let's have a look:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;But, with promise, we use the callback on returned promise instead to form the promise chain which makes code better and more readable.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;catch&lt;/code&gt; looks new here, right? Ok. so catch is another method provided by promise which catches any kind of error that might have happened while promise tries to fulfil.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is completely possible to chain after &lt;code&gt;catch&lt;/code&gt; using &lt;code&gt;then&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Promise.prototype.finally
&lt;/h2&gt;

&lt;p&gt;This is again part of promise &lt;code&gt;Promise.prototype.finally&lt;/code&gt;. It is useful when we wants to execute some sort of code regardless the outcome of the promise (be it fulfilled or rejected). Syntax is given below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's move to our next point which is &lt;code&gt;async/await&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  async/await
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; is nothing more than syntactic sugar for promises that we learnt above. Let's look at the syntax first:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's say we need to fetch json file from a url. Here is how we will write it using &lt;code&gt;promise&lt;/code&gt;.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getJson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;// call the getJson method&lt;/span&gt;
&lt;span class="nx"&gt;getJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://jsonplaceholder.typicode.com/todos/1'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, let's have a look on how we can use &lt;code&gt;async/await&lt;/code&gt; for this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Both implementation are doing the same thing but &lt;code&gt;async/await&lt;/code&gt; is more readable. As I have mentioned before about chaining, &lt;code&gt;promise/then&lt;/code&gt; makes code less readable when we start doing the chaining but with &lt;code&gt;async/await&lt;/code&gt;, you get more neat and clean code. Example given below to explain it. Here, I have created three promises:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now have a look at the usage of promise and async/await&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;promise&lt;/code&gt; way&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;async/await&lt;/code&gt; way:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, which one you prefer. Let me know in the comment section. Personally, I like &lt;code&gt;async/await&lt;/code&gt;. It makes code more readable in synchronous way which leads to more manageable code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;await&lt;/code&gt; can only be used inside &lt;code&gt;async&lt;/code&gt; function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Go ahead and do practice this at your own. Have questions, feel free to drop a comment with your question.&lt;/p&gt;

&lt;h2&gt;
  
  
  for...await...of
&lt;/h2&gt;

&lt;p&gt;This one is related to &lt;code&gt;await&lt;/code&gt; that we learnt just now. This helps us to iterate over async iterable objects as well as on sync iterables which includes built in string, array or array like objects (eg. &lt;code&gt;arguments&lt;/code&gt;). Syntax:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="kr"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// code goes here...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Let's take an example. Suppose, we need to fetch data from 3 urls. The urls are stored in an array.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I am sure you also realise how clean it is and how useful it can be. So, practice, practice and practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Today we learnt about &lt;code&gt;Promise&lt;/code&gt;. The newer and nicer way to use the &lt;code&gt;Promise&lt;/code&gt; by using &lt;code&gt;async/await&lt;/code&gt;. We also looked at &lt;code&gt;finally&lt;/code&gt; along with how to iterate on &lt;code&gt;await&lt;/code&gt; using &lt;code&gt;for...await...of&lt;/code&gt;. Some of the terminology to remember are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promise&lt;/li&gt;
&lt;li&gt;resolve/fulfilled&lt;/li&gt;
&lt;li&gt;reject&lt;/li&gt;
&lt;li&gt;settled&lt;/li&gt;
&lt;li&gt;chaining&lt;/li&gt;
&lt;li&gt;then&lt;/li&gt;
&lt;li&gt;catch&lt;/li&gt;
&lt;li&gt;finally&lt;/li&gt;
&lt;li&gt;async&lt;/li&gt;
&lt;li&gt;await&lt;/li&gt;
&lt;li&gt;for...await...of&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are chances that you have questions around some of the points above which is not explained. It is kind of done intentionally so that you can come up with questions and also start exploring at your own. For any questions, drop a comment and own awesome community folks or myself will help you to get it answered.&lt;/p&gt;

&lt;p&gt;Here are two questions from me for you to answer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q1: How to use multiple promises in parallel?&lt;/p&gt;

&lt;p&gt;Q2: How can we solve &lt;code&gt;Q1&lt;/code&gt; with &lt;code&gt;async/await&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading. Happy learning.&lt;/p&gt;

&lt;p&gt;--&lt;br&gt;
&lt;em&gt;Originally published at &lt;a href="https://elanandkumar.com/blog/es6-and-beyond-II"&gt;https://elanandkumar.com/blog/es6-and-beyond-II&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>es6</category>
      <category>javascript</category>
      <category>intermediate</category>
      <category>promises</category>
    </item>
  </channel>
</rss>
