<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: João Guilherme do Amaral Vequiato</title>
    <description>The latest articles on Forem by João Guilherme do Amaral Vequiato (@jgamaraalv).</description>
    <link>https://forem.com/jgamaraalv</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%2F321084%2F94b5b56f-ff0f-47c2-ba9b-beaa33da76ea.jpeg</url>
      <title>Forem: João Guilherme do Amaral Vequiato</title>
      <link>https://forem.com/jgamaraalv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jgamaraalv"/>
    <language>en</language>
    <item>
      <title>You DON'T know Reactjs - Part I</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Tue, 11 Apr 2023 15:46:39 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/you-dont-know-reactjs-part-i-342p</link>
      <guid>https://forem.com/jgamaraalv/you-dont-know-reactjs-part-i-342p</guid>
      <description>&lt;h3&gt;
  
  
  A brief reflection
&lt;/h3&gt;

&lt;p&gt;One thing that deeply bothers me these days are course sellers who promise easy jobs and exorbitant earnings, but whose content offered is so shallow and simplistic that they only teach people to be mere spectators of code - to look, copy, and paste. However, this is not always the instructors' fault.&lt;/p&gt;

&lt;p&gt;In fact, there are several courses on the market that are excellent for getting a kick-start and allowing you to quickly position yourself in the job market - I myself achieved this through this method.&lt;/p&gt;

&lt;p&gt;However, many aspiring developers (and some even with senior positions) are content to just consume these contents, replicate, make a &lt;code&gt;to-do&lt;/code&gt; list, and that's it. They don't know what happens behind the scenes.&lt;/p&gt;

&lt;p&gt;Not that understanding the intricacies behind everything is essential to work in the field, but it is certainly essential for you to stand out as a sought-after developer in the job market.&lt;/p&gt;

&lt;p&gt;Some time ago, I helped two friends take tests for a first job who had done courses that promised that you would become full-stack in 4 weeks. It's scary to see that these courses make people complete 5 different projects and they leave the course without knowing the basics.&lt;/p&gt;

&lt;p&gt;It's a huge false sense of learning, and the method is always the same: you see the guy coding a project x, do the same while watching and you're done. You have your first system, easy and simple as baking a cake.&lt;/p&gt;

&lt;p&gt;Thinking about this, I decided to write this article to assist - those who want to get out of mediocrity - and to help Reactjs developers understand, at least a little, what React does behind the scenes.&lt;/p&gt;

&lt;p&gt;Let's go?&lt;/p&gt;

&lt;h3&gt;
  
  
  Fundamental concepts of Reactjs
&lt;/h3&gt;

&lt;p&gt;Can you tell me in a few words what React is today? - take a minute and try to answer in your own words.&lt;/p&gt;

&lt;p&gt;I don't want to get into technical definitions here, so I'll use my own words too.&lt;/p&gt;

&lt;p&gt;React is a &lt;strong&gt;library&lt;/strong&gt; that allows you to write performant and easy-to-maintain code through the concept of &lt;strong&gt;reusable components&lt;/strong&gt;, where you can describe &lt;strong&gt;user interfaces declaratively&lt;/strong&gt;, without worrying about implementation details.&lt;/p&gt;

&lt;p&gt;Is it difficult to understand? Let's try an easier definition:&lt;/p&gt;

&lt;p&gt;React is a badass &lt;strong&gt;library&lt;/strong&gt; for writing performant and easy-to-maintain &lt;strong&gt;declarative&lt;/strong&gt; UIs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Declarative programming vs imperative programming
&lt;/h4&gt;

&lt;p&gt;But what would be a &lt;strong&gt;declarative&lt;/strong&gt; way of writing a UI?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;declarative&lt;/strong&gt; way is the way React works. Basically, you &lt;strong&gt;declare&lt;/strong&gt; what you would like to happen in the user interface when a certain action is called.&lt;/p&gt;

&lt;p&gt;Unlike the &lt;strong&gt;imperative&lt;/strong&gt; way, which instead of describing, you have to &lt;strong&gt;command&lt;/strong&gt; that it be done.&lt;/p&gt;

&lt;p&gt;Let's use an example with React (in a declarative way) and the same example of how it would be in an imperative way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declarative way:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&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;MyApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;buttonClickHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonClickHandler&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoading&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;isLoading&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loading...&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;Click me&lt;/span&gt;&lt;span class="dl"&gt;'&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;buton&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;In the example above, we &lt;strong&gt;declare&lt;/strong&gt; that when the user clicks the button, the user interface should update the text to "loading" and disable it.&lt;/p&gt;

&lt;p&gt;We don't have to worry about how it will do this, just show it which interface we want when a certain action occurs and it will figure out how to make it happen.&lt;/p&gt;

&lt;p&gt;How would the same code look in an &lt;strong&gt;imperative way&lt;/strong&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button&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;buttonClickHandler&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;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;disabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loading...&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;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttonClickHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to worry about every detail we want when the user clicks the button, such as disabling it manually, changing its text manually, and even creating the button itself and inserting it into the DOM.&lt;/p&gt;

&lt;p&gt;With our friend React, all of this logic is abstracted.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML and the DOM
&lt;/h3&gt;

&lt;p&gt;In the end, everything we do in React serves a single purpose: rendering HTML elements in the &lt;code&gt;DOM (Document Object Model)&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is the DOM?
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;DOM&lt;/code&gt; is a hierarchical tree representing HTML elements, used by web browsers.&lt;/p&gt;

&lt;p&gt;In simpler terms, it's everything the user sees on the browser screen.&lt;/p&gt;

&lt;p&gt;It's the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; tags and their respective children that you put on your webpage.&lt;/p&gt;

&lt;p&gt;When you write an HTML page (or your React components, which will ultimately also be an HTML page), this is how your browser interprets each element.&lt;/p&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.vequiato.dev%2Fdocument-object-model.jpeg" class="article-body-image-wrapper"&gt;&lt;img alt="Document Object Model" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.vequiato.dev%2Fdocument-object-model.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the DOM.&lt;/p&gt;

&lt;h4&gt;
  
  
  But why React?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Javascript&lt;/strong&gt; is the easiest and most common way to manipulate the DOM, whether to add or remove elements, add or remove attributes, add interactivity, among others.&lt;/p&gt;

&lt;p&gt;However, directly interacting with the DOM and "painting" these interactions on the screen is very expensive, which can bring a poor experience and many performance problems.&lt;/p&gt;

&lt;p&gt;React - in addition to making our lives easier with the form of &lt;strong&gt;declarative programming&lt;/strong&gt; and the possibility of reusing &lt;strong&gt;components&lt;/strong&gt; - has a very intelligent algorithm of &lt;strong&gt;reconciliation&lt;/strong&gt; allied with its &lt;strong&gt;Virtual DOM&lt;/strong&gt;, which can make this process much more performative by making the minimum possible changes directly in the user's &lt;code&gt;DOM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But that's a topic for part II of this article series (&lt;del&gt;if it exists&lt;/del&gt;).&lt;/p&gt;

&lt;p&gt;Comment below what you thought of this content, if you liked the way it was written and if you want me to keep writing about it.&lt;/p&gt;

&lt;p&gt;See ya! 👋&lt;/p&gt;

&lt;h4&gt;
  
  
  Did you like it? Check out other articles written by me:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.vequiato.dev/blog/maximize-your-learning-and-productivity-with-this-chrome-extension-nhj/1431622" rel="noopener noreferrer"&gt;Maximize your learning and productivity with this Chrome extension&lt;/a&gt; &lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/react-render-props-pattern-314j/1426266" rel="noopener noreferrer"&gt;React Render Props Pattern&lt;/a&gt; &lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/6-react-questions-for-technical-interviews-and-their-answers-10p3/1318608" rel="noopener noreferrer"&gt;6 React questions for technical interviews and their answers&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/understanding-closures-h2b/245969" rel="noopener noreferrer"&gt;Understanding closures&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/understanding-classes-and-prototypes-5hhf/245484" rel="noopener noreferrer"&gt;Understanding classes and prototypes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/understanding-higher-order-function-1dlg/250804" rel="noopener noreferrer"&gt;Understanding Higher Order Function&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Maximize your learning and productivity with this Chrome extension</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Mon, 10 Apr 2023 14:37:15 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/maximize-your-learning-and-productivity-with-this-chrome-extension-nhj</link>
      <guid>https://forem.com/jgamaraalv/maximize-your-learning-and-productivity-with-this-chrome-extension-nhj</guid>
      <description>

&lt;h2&gt;
  
  
  PinMyVideo
&lt;/h2&gt;

&lt;p&gt;Watch your favorite videos in a popup while browsing the web without losing focus.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;I don't know about you, but whenever I'm working, I like to play some interesting podcast or technology classes and workshops to listen to while not listening to music.&lt;/p&gt;

&lt;p&gt;The big problem is that I can't focus on both things at the same time, so 90% of my focus is on the task I'm working on and 10% on the video I'm listening to.&lt;/p&gt;

&lt;p&gt;Sometimes, I even get lost in what I'm listening to and focus only on the task, forgetting that there's a video playing.&lt;/p&gt;

&lt;p&gt;And then, when I hear something interesting in the video or something I don't know yet and want to learn, I immediately stop the task to go to the video and see what's being explained.&lt;/p&gt;

&lt;p&gt;The problem with this is that I always end up losing focus on the task when I choose to go to the video, and sometimes I spend minutes searching for the video among thousands of open tabs.&lt;/p&gt;

&lt;p&gt;That's when I thought: &lt;em&gt;what if I did something that could keep the video playing without having to keep the video tab open, while I do my tasks normally, just like YouTube Premium for mobile or Twitch?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, whenever I heard something interesting, the video would already be there in front of me to watch. Or even while thinking about my task, the video would also be there in front of me to follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;I made this extension and tested it for a week to see if it would be useful for me. I spent a week watching JavaScript classes on video.&lt;/p&gt;

&lt;p&gt;The result using the extension was that not only did I notice an improvement in learning the subject matter of the video, as the video was always there in front of me, so it didn't become just a lost audio that I forgot was playing (I am a very visual person), but I also completed more tasks than usual, as I didn't have to keep searching and going back to the video tab all the time, searching for the exact minute of the subject I wanted to listen to and losing minutes in the video tab to go back to the task I was doing (and often forgetting where I was in the task).&lt;/p&gt;

&lt;p&gt;So I decided to make this extension available to everyone, as it helped me and it can help others too.&lt;/p&gt;

&lt;p&gt;I called this extension &lt;a href="https://chrome.google.com/webstore/detail/pinmyvideo/cpihinngdiofancfpfghfogkolilehfg?hl=pt-BR&amp;amp;authuser=0"&gt;&lt;strong&gt;PinMyVideo&lt;/strong&gt;&lt;/a&gt;, and its appeal is precisely this: &lt;strong&gt;"Watch your favorite videos in a popup while browsing the web without losing focus."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Link to the extension:&lt;/strong&gt; &lt;a href="https://chrome.google.com/webstore/detail/pinmyvideo/cpihinngdiofancfpfghfogkolilehfg?hl=pt-BR&amp;amp;authuser=0"&gt;https://chrome.google.com/webstore/detail/pinmyvideo/cpihinngdiofancfpfghfogkolilehfg?hl=pt-BR&amp;amp;authuser=0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope it is useful for you, as it has been for me, and I am open to hearing criticism and suggestions to make it even better!&lt;/p&gt;

&lt;p&gt;Try it out and let me know what you think. If it's useful, leaving a comment there with 5 stars will be very welcome.&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>[React] Render Props Pattern</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Tue, 04 Apr 2023 20:44:37 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/react-render-props-pattern-314j</link>
      <guid>https://forem.com/jgamaraalv/react-render-props-pattern-314j</guid>
      <description>&lt;h3&gt;
  
  
  What is render props pattern? Understand this powerful React pattern.
&lt;/h3&gt;

&lt;p&gt;With the Render Props pattern, we pass &lt;em&gt;components&lt;/em&gt; as props to other &lt;em&gt;components&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;components&lt;/em&gt; that are passed as &lt;em&gt;props&lt;/em&gt; can, in turn, receive other &lt;em&gt;props&lt;/em&gt; from that parent &lt;em&gt;component&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Render props&lt;/em&gt; make it easy to reuse logic across multiple components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;If we wanted to implement an input with which a user can convert a Celsius temperature to Kelvin and Fahrenheit, we can use the &lt;strong&gt;renderKelvin&lt;/strong&gt; and &lt;strong&gt;renderFahrenheit&lt;/strong&gt; render props.&lt;/p&gt;

&lt;p&gt;These &lt;em&gt;props&lt;/em&gt; receive the input value, which they convert to the correct temperature in K or °F.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Input&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="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;input&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;renderKelvin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;273.15&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="si"&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;renderFahrenheit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;
      &lt;span class="na"&gt;renderKelvin&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;value&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"temp"&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;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;K&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="na"&gt;renderFahrenheit&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;value&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"temp"&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;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;°F&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="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;h3&gt;
  
  
  Benefits
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reuse&lt;/strong&gt;: Since render props can be different every time, we can make components that receive render props highly reusable for multiple use cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separation of concerns&lt;/strong&gt;: We can separate the logic of our application from the rendering components through render props. The stateful component that receives a render prop can pass the data to stateless components, which only render the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution to HOC problems&lt;/strong&gt;: Since we pass props explicitly, we solve the problem of implicit props from HOCs. The props that need to be passed to the element are all visible in the render prop's argument list. We know exactly where certain props come from.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Drawbacks
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Not as necessary with hooks&lt;/strong&gt;: Hooks have changed the way we can add reuse and data sharing to components, which can replace the render props pattern in many cases. However, there are still cases where the render props pattern can be the best solution.&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;Comment below if you knew about this pattern and if you use it in your daily work!&lt;/p&gt;



&lt;h4&gt;
  
  
  Did you like it? Check out other articles written by me:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.vequiato.dev/blog/understanding-closures-h2b/245969"&gt;Understanding closures&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/understanding-classes-and-prototypes-5hhf/245484"&gt;Understanding classes and prototypes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.vequiato.dev/blog/understanding-higher-order-function-1dlg/250804"&gt;Understanding Higher Order Function&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>6 React questions for technical interviews and their answers</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Thu, 05 Jan 2023 16:21:21 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/6-react-questions-for-technical-interviews-and-their-answers-10p3</link>
      <guid>https://forem.com/jgamaraalv/6-react-questions-for-technical-interviews-and-their-answers-10p3</guid>
      <description>&lt;p&gt;Throughout my career as a developer I have been able to go through some selection processes, with different difficulties and questions about certain subjects.&lt;/p&gt;

&lt;p&gt;I decided to gather in this post the most common questions and their respective answers.&lt;/p&gt;

&lt;p&gt;Not for them to be decorated, but because I think they are extremely important to understand for anyone looking to evolve in their career as a React developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is the difference between Component and PureComponent?
&lt;/h2&gt;

&lt;p&gt;A component in React is a function or class that returns a React element, which is a description of a user interface. &lt;/p&gt;

&lt;p&gt;A pure component is a component that implements the &lt;code&gt;shouldComponentUpdate&lt;/code&gt; lifecycle method in a way that prevents the component from re-rendering if the component's props and state have not changed.&lt;/p&gt;

&lt;p&gt;Here is an example of a simple component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function HelloWorld(props) {
  return &amp;lt;div&amp;gt;Hello, {props.name}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an example of the same component implemented as a pure component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class HelloWorld extends React.PureComponent {
  render() {
    return &amp;lt;div&amp;gt;Hello, {this.props.name}&amp;lt;/div&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main difference between a component and a pure component is that a pure component implements &lt;code&gt;shouldComponentUpdate&lt;/code&gt; with a shallow prop and state comparison, while a component does not. &lt;/p&gt;

&lt;p&gt;This means that a pure component will not re-render if its props and state have not changed, while a component will always re-render every time it is rendered.&lt;/p&gt;

&lt;p&gt;Using a pure component can improve performance in cases where the component is rendered frequently and the component's props and state do not change often. &lt;/p&gt;

&lt;p&gt;However, if the component's props or state do change frequently, using a pure component can prevent the component from updating when it should, which can break the app.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Describe 3 ways to pass information from a component to its &lt;strong&gt;parent&lt;/strong&gt;.
&lt;/h2&gt;

&lt;p&gt;There are several ways to pass information from a child component to its parent in React:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback functions:&lt;/strong&gt; You can pass a callback function as a prop to the child component. The child component can then call the callback function when it needs to pass data back to the parent. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In the parent component:

function Parent() {
  const handleChildData = data =&amp;gt; {
    // Do something with the data from the child component
  };

  return &amp;lt;Child onChildData={handleChildData} /&amp;gt;;
}

// In the child component:

function Child(props) {
  const handleClick = () =&amp;gt; {
    const data = "some data";
    props.onChildData(data);
  };

  return &amp;lt;button onClick={handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; You can use the React context feature to pass data from a child component to a parent component. The parent component creates a context object and the child component consumes it. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In the parent component:

const DataContext = React.createContext();

function Parent() {
  const [data, setData] = React.useState(null);

  return (
    &amp;lt;DataContext.Provider value={[data, setData]}&amp;gt;
      &amp;lt;Child /&amp;gt;
    &amp;lt;/DataContext.Provider&amp;gt;
  );
}

// In the child component:

function Child() {
  const [data, setData] = React.useContext(DataContext);

  const handleClick = () =&amp;gt; {
    setData("some data");
  };

  return &amp;lt;button onClick={handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ref:&lt;/strong&gt; We can pass a ref to the children component, so we can acess the value with &lt;code&gt;ref.current&lt;/code&gt; in the parent component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const inputReft = useRef();

  &amp;lt;InputComponent ref={inputRef}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Give 2 ways to prevent components from re-rendering.
&lt;/h2&gt;

&lt;p&gt;There are several ways to prevent a component from re-rendering in React:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React.PureComponent:&lt;/strong&gt; As mentioned previously, you can use React.PureComponent instead of React.&lt;/p&gt;

&lt;p&gt;Component when defining a component class. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;React.PureComponent&lt;/code&gt; implements the &lt;code&gt;shouldComponentUpdate&lt;/code&gt; lifecycle method with a shallow prop and state comparison, which means that the component will only re-render if its props or state have changed. &lt;/p&gt;

&lt;p&gt;If the props and state are the same according to the comparison, the component will not re-render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyComponent extends React.PureComponent {
  render() {
    // Render the component
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React.memo:&lt;/strong&gt; You can use the &lt;code&gt;React.memo&lt;/code&gt; higher-order component to wrap a function component and prevent it from re-rendering if its props have not changed. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;React.memo&lt;/code&gt; performs a shallow comparison of the props to determine if the component should re-render. &lt;/p&gt;

&lt;p&gt;If the props are the same according to the comparison, the component will not re-render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyComponent = React.memo(function MyComponent(props) {
  // Render the component
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also provide a custom comparison function to &lt;code&gt;React.memo&lt;/code&gt; if you need to perform a more complex comparison of the props. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const areEqual = (prevProps, nextProps) =&amp;gt; {
  // Return true if the props are equal, false otherwise
};

const MyComponent = React.memo(function MyComponent(props) {
  // Render the component
}, areEqual);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. How many arguments does &lt;code&gt;setState&lt;/code&gt; take and why is it &lt;code&gt;async&lt;/code&gt;.
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;setState&lt;/code&gt; method in React takes up to two arguments. &lt;/p&gt;

&lt;p&gt;The first argument is an updater function or an object representing the new state. &lt;/p&gt;

&lt;p&gt;The second argument is an optional callback function that is called after the state has been updated.&lt;/p&gt;

&lt;p&gt;Here is an example of using &lt;code&gt;setState&lt;/code&gt; with an updater function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState(prevState =&amp;gt; {
  return {
    count: prevState.count + 1
  };
});

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

&lt;/div&gt;



&lt;p&gt;Here is an example of using setState with an object representing the new state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState({
  count: this.state.count + 1
});

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;setState&lt;/code&gt; is asynchronous because it may be batched together with other state updates, which means that the state may not be updated immediately after calling &lt;code&gt;setState&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is done to improve performance by avoiding unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;If you need to perform an action after the state has been updated, you can pass a callback function as the second argument to &lt;code&gt;setState&lt;/code&gt;. The callback function will be called after the state has been updated and the component has re-rendered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState(
  {
    count: this.state.count + 1
  },
  () =&amp;gt; {
    // Do something after the state has been updated
  }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to note that the state may be updated asynchronously, so you should not rely on the state being immediately available after calling &lt;code&gt;setState&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you need to use the updated state in a subsequent update, you should pass a function as the first argument to &lt;code&gt;setState&lt;/code&gt; and use the current state as an argument to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState(prevState =&amp;gt; {
  return {
    count: prevState.count + 1
  };
});

// The state may not be immediately available, so it is important to use
// the previous state as an argument to the updater function
this.setState(prevState =&amp;gt; {
  return {
    count: prevState.count + 1
  };
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. What is a fragment and why do we need it?
&lt;/h2&gt;

&lt;p&gt;In React, a fragment is a way to group a list of children without adding extra nodes to the DOM. &lt;/p&gt;

&lt;p&gt;Fragments are useful when you want to return multiple elements from a component's render method, but you don't want to wrap the elements in an extra DOM node.&lt;/p&gt;

&lt;p&gt;Here is an example of using a fragment to group a list of elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;p&amp;gt;Item 1&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Item 2&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Item 3&amp;lt;/p&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;This will render the same JSX as the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
  return (
    &amp;lt;React.Fragment&amp;gt;
      &amp;lt;li&amp;gt;Item 1&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;Item 2&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;Item 3&amp;lt;/li&amp;gt;
    &amp;lt;/React.Fragment&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Both of these examples will render a list with three items, but the fragment allows you to group the elements without adding an extra node to the DOM. &lt;/p&gt;

&lt;p&gt;This can be useful in cases where you want to return multiple elements from a component, but you don't want to wrap the elements in an extra node.&lt;/p&gt;

&lt;p&gt;In the past, you could use a div element as a placeholder for a fragment, but this is no longer recommended because it can lead to confusion and can cause problems with CSS. &lt;/p&gt;

&lt;p&gt;It is now recommended to use fragments or a &lt;code&gt;React.Fragment&lt;/code&gt; element to group elements in a list.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. List the steps needed to migrate a Class to Function Component.
&lt;/h2&gt;

&lt;p&gt;To migrate a class component to a function component in React, you can follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert the class component to a function component. To do this, you will need to remove the &lt;code&gt;class&lt;/code&gt; keyword and the extends clause, and add the &lt;code&gt;function&lt;/code&gt; keyword. You should also remove the &lt;code&gt;render&lt;/code&gt; method and move the JSX code to the top level of the component function.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Before:
class MyComponent extends React.Component {
  render() {
    return &amp;lt;div&amp;gt;Hello, world!&amp;lt;/div&amp;gt;;
  }
}

// After:
function MyComponent(props) {
  return &amp;lt;div&amp;gt;Hello, world!&amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Convert any instance variables or methods to function component hooks. If the class component uses any instance variables or methods, you will need to convert them to function component hooks. This includes variables like &lt;code&gt;this.state&lt;/code&gt; and &lt;code&gt;this.props&lt;/code&gt;, as well as methods like &lt;code&gt;this.setState&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Before:
class MyComponent extends React.Component {
  state = { count: 0 };

  handleClick = () =&amp;gt; {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={this.handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;
        &amp;lt;p&amp;gt;{this.state.count}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

// After:
function MyComponent() {
  const [count, setCount] = React.useState(0);

  const handleClick = () =&amp;gt; {
    setCount(count + 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;
      &amp;lt;p&amp;gt;{count}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Remove the &lt;code&gt;constructor&lt;/code&gt; method. If the class component has a &lt;code&gt;constructor&lt;/code&gt; method, you can remove it, since function components do not have a constructor method.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Before:
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    // Initialize state and bind methods
  }

  // Other methods and render function
}

// After:
function MyComponent(props) {
  // Initialize state using hooks and define methods as functions
  // Render function
}

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Convert any &lt;code&gt;static&lt;/code&gt; methods or properties to regular &lt;code&gt;functions&lt;/code&gt; or &lt;code&gt;variables&lt;/code&gt;. If the &lt;code&gt;class&lt;/code&gt; component has any &lt;code&gt;static&lt;/code&gt; methods or properties, you will need to convert them to regular &lt;code&gt;functions&lt;/code&gt; or &lt;code&gt;variables&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Before:
class MyComponent extends React.Component {
  static propTypes = {
    name: PropTypes.string.isRequired
  };

  static defaultProps = {
    name: "world"
  };

  // Other methods and render function
}

// After:
MyComponent.propTypes = {
  name: PropTypes.string.isRequired
};

MyComponent.defaultProps = {
  name: "world"
};

function MyComponent(props) {
  // Render function
}

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

&lt;/div&gt;



&lt;p&gt;After following these steps, your class component should be fully converted to a function component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extra (a Javascript question) - What's the difference in handling exceptions in promises, callbacks and async...await.
&lt;/h2&gt;

&lt;p&gt;In JavaScript, there are three main ways to handle exceptions: using try...catch with synchronous code, using a callback function, and using async/await. Here is a brief overview of the differences between these three approaches:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;try...catch:&lt;/strong&gt; &lt;code&gt;try...catch&lt;/code&gt; is used to handle exceptions in synchronous code. When an exception is thrown in the try block, the code execution is immediately halted and the catch block is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
  // Code that may throw an exception
} catch (error) {
  // Handle the exception
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Callbacks:&lt;/strong&gt; Callbacks are used to handle asynchronous exceptions. When an asynchronous function returns a result or an error, it calls a callback function with the result or the error as an argument. The callback function can then handle the result or the error as needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;asyncFunction(function(error, result) {
  if (error) {
    // Handle the error
  } else {
    // Use the result
  }
});

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;async/await:&lt;/strong&gt; &lt;code&gt;async/await&lt;/code&gt; is a more recent approach to handling asynchronous exceptions. &lt;code&gt;async&lt;/code&gt; functions allow you to use the &lt;code&gt;await&lt;/code&gt; keyword to pause the execution of the function until a promise is resolved. If the promise is rejected, an exception is thrown, which can be handled using a &lt;code&gt;try...catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example() {
  try {
    const result = await asyncFunction();
    // Use the result
  } catch (error) {
    // Handle the error
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In general, &lt;code&gt;try...catch&lt;/code&gt; is used to handle exceptions in synchronous code, while call&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Understanding Higher Order Function</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Wed, 29 Jan 2020 02:52:20 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/understanding-higher-order-function-1dlg</link>
      <guid>https://forem.com/jgamaraalv/understanding-higher-order-function-1dlg</guid>
      <description>&lt;p&gt;Higher Order Function it's a very basic concept (and much important too) but also a little misunderstood.&lt;/p&gt;

&lt;p&gt;Let's understand first the concept, the most clear definition, simple and direct that you probably can use in some tecnical interview.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Higher Order Function? (aka HOF)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Higher Order Function it's all function that acepts other function as a parameter and/or return one function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we go for a more deep explanation, we can list some important things about HOFs in programing.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Makes our code more declaritive and easy to read.&lt;/li&gt;
&lt;li&gt;It's the key concept to more complex concepts in programing, like closures, currying, promises, and others.&lt;/li&gt;
&lt;li&gt;We can generalize functions, so we can reuse these in different ways when it's necessary.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Understanding the Higher Order Functions
&lt;/h3&gt;

&lt;p&gt;See the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function copyArrayAndMultiplyBy5(array) {
  const output = [];
  for (let i = 0; i &amp;lt; array.length; i++) {
    output.push(array[i] * 5);
  }
  return output;
}
const myArray = [1,2,3];
const result = copyArrayAndMultiplyBy5(myArray);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose that we want do now one multiplication function for 3, 4.&lt;br&gt;
Or maybe addition, or division. We need to copy and paste various functions, change only the name and the values from 5 to 3, 4 and too the operator from * to + and /.&lt;/p&gt;

&lt;p&gt;And if, rather than define in the declaration moment the all behavior of our function, could we define one parameter to receive another function as argument and let the behavior to the execution moment?&lt;/p&gt;

&lt;p&gt;In Javascript functions are objects, called first class objects.&lt;/p&gt;

&lt;p&gt;If we can pass objects as arguments for one function, we can pass another function too.&lt;br&gt;
Let's edit our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function copyArrayAndManipulate(array, fn) {
  const output = [];
  for (let i = 0; i &amp;lt; array.length; i++) {
    output.push(fn(array[i]));
  }
  return output;
}

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

&lt;/div&gt;



&lt;p&gt;Now we create a generic function &lt;strong&gt;&lt;em&gt;copyArrayAndManipulate&lt;/em&gt;&lt;/strong&gt;, that receives as parameter the array e one function called &lt;strong&gt;&lt;em&gt;fn&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We don't know all the function's behavior on your declaration moment, and we don't need to know.&lt;/p&gt;

&lt;p&gt;The function &lt;strong&gt;&lt;em&gt;copyArrayAndManipulate&lt;/em&gt;&lt;/strong&gt; it's our Higher Order Function, while the function &lt;strong&gt;&lt;em&gt;fn&lt;/em&gt;&lt;/strong&gt; that we'll pass in argument is our callback function.&lt;/p&gt;

&lt;p&gt;Now we can create our variations in a much easier and dynamic way, without having to copy and paste unnecessary code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function copyArrayAndManipulate(array, fn) {
  const output = [];
  for (let i = 0; i &amp;lt; array.length; i++) {
    output.push(fn(array[i]));
  }
  return output;
}
function multiplyBy5(input) { 
  return input * 5;
}
function multiplyBy3(input) { 
  return input * 3;
}
function multiplyBy4(input) { 
  return input * 4;
}
const result1 = copyArrayAndManipulate([1, 2, 3], multiplyBy5);
const result2 = copyArrayAndManipulate([1, 2, 3], multiplyBy3);
const result3 = copyArrayAndManipulate([1, 2, 3], multiplyBy4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our code stayed much more declarative and legible, because in reading we know exactly that the value of result1 will be the copy of array [1, 2, 3] multiplied by 5, for example.&lt;/p&gt;

&lt;p&gt;Ok, this already improve writing of our code avoiding unnecessary code, but we can let the aesthetic better with arrow functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function copyArrayAndManipulate(array, fn) {
  const output = [];
  for (let i = 0; i &amp;lt; array.length; i++) {
    output.push(fn(array[i]));
  }
  return output;
}
const multiplyBy5 = (input) =&amp;gt; input * 5;
const multiplyBy3 = (input) =&amp;gt; input * 3;
const multiplyBy4 = (input) =&amp;gt; input * 4;
const result1 = copyArrayAndManipulate([1, 2, 3], multiplyBy5);
const result2 = copyArrayAndManipulate([1, 2, 3], multiplyBy3);
const result3 = copyArrayAndManipulate([1, 2, 3], multiplyBy4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So much better. But we don't need to store our functions in constants, we can pass these directly. This give to us more dinamism.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function copyArrayAndManipulate(array, fn) {
  const output = [];
  for (let i = 0; i &amp;lt; array.length; i++) {
    output.push(fn(array[i]));
  }
  return output;
}
const result1 = copyArrayAndManipulate([1, 2],(input) =&amp;gt; input * 5);
const result2 = copyArrayAndManipulate([1, 2],(input) =&amp;gt; input * 3);
const result3 = copyArrayAndManipulate([1, 2],(input) =&amp;gt; input * 4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most attentive must have noticed a similarity with &lt;strong&gt;&lt;em&gt;map&lt;/em&gt;&lt;/strong&gt; function.&lt;br&gt;
Yes, it's exactly how it works. And as map we have another functions very used and known that be HOFs (like &lt;strong&gt;&lt;em&gt;filter&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;reduce&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Higher Order Function it's a simple and important concept, but so much peoples have a little difficult to understand it.&lt;/p&gt;

&lt;p&gt;I hope have achieved desmistify this with the better possible way.&lt;/p&gt;

&lt;p&gt;See you soon!👋&lt;/p&gt;

&lt;p&gt;If you lost:&lt;br&gt;
&lt;a href="https://vequiato.dev/blog/understanding-classes-and-prototypes-5hhf/245484"&gt;Understanding classes and prototypes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://vequiato.dev/blog/understanding-closures-h2b/245969"&gt;Understanding closures&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding closures</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Tue, 21 Jan 2020 23:56:17 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/understanding-closures-h2b</link>
      <guid>https://forem.com/jgamaraalv/understanding-closures-h2b</guid>
      <description>&lt;p&gt;Quickly definition to who come from google searching "What is closure"&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, it's a function that is able to remember the environment that was created.&lt;/p&gt;

&lt;p&gt;Also known as &lt;strong&gt;&lt;em&gt;"Closed over Variable Ambiente"&lt;/em&gt;&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;C.O.V.E&lt;/em&gt;&lt;/strong&gt;) and &lt;strong&gt;&lt;em&gt;"Persistent Lexical Scope Referenced Data"&lt;/em&gt;&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;P.L.S.R.D&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Ok, but what that means?&lt;/p&gt;

&lt;p&gt;When we execute a function in Javascript, that function creates a brand new one execution context to it, with a local memory, variable ambiente and a state.&lt;/p&gt;

&lt;p&gt;What happens is that when the function execution it's concluded, all of this context is excluded, including your local memory (which owns all of arguments that we passed for this function). Except the value that it returns.&lt;/p&gt;

&lt;p&gt;But what if we could create "functions with memories". Functions that able persist data, like if could we store a state for it. This is the one of the most powerful concepts in Javascript, closures.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions with memories 🧠
&lt;/h3&gt;

&lt;p&gt;This can be a little abstract in this point (how the closures can be so powerful), but keep in your mind in this point that so much concepts using closures under the hood, like memoization functions, module pattern, iterator, currying and much more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Applying closures concept 🔥
&lt;/h3&gt;

&lt;p&gt;Pay attention in the codes below, what we'll do it's called function decorator, which make us able to "edit" our functions (this is not the moment topic, soon i'll make one article series about functional programming).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiplyBy2 = (number) =&amp;gt; number * 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, nothing new until now.&lt;br&gt;
Suppose we need to make with that our multiplication function, in a certain context, can be used only one time.&lt;/p&gt;

&lt;p&gt;Can we create a &lt;strong&gt;&lt;em&gt;counter&lt;/em&gt;&lt;/strong&gt; inside of function for in the case when &lt;strong&gt;&lt;em&gt;counter &amp;gt; 0&lt;/em&gt;&lt;/strong&gt; it doesn't execute? This is not possible, because every time that function ends its execution its local memory is destroyed.&lt;/p&gt;

&lt;p&gt;So, lets create a new function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const oncefy = (fn) =&amp;gt; {
  const counter = 0;
  const myFunction = () =&amp;gt; {
   if (counter === 0) {
     fn();
     return;
   }
   console.log("Only one time");
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our function &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt; receives one function like parameter, define a counter and verify if counter is equal 0, if is equal 0 execute our argument, else our function prints in the console.&lt;/p&gt;

&lt;p&gt;Let's apply the closure concept in our function &lt;strong&gt;&lt;em&gt;multiplyBy2&lt;/em&gt;&lt;/strong&gt;, passing it like argument for our function &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt;, wich is responsable to memorizer our &lt;strong&gt;&lt;em&gt;counter&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiplyBy2 = (number) =&amp;gt; {
  console.log(number * 2);
}
const oncefy = (fn) =&amp;gt; {
  let counter = 0;
  const myFunction = (number) =&amp;gt; {
    if (counter === 0) {
      fn(number); 
      counter++; 
      return;
    } 
    console.log("🍃");
  }
  return myFunction;
}
const multiplyBy2Once = oncefy(multiplyBy2);
multiplyBy2Once(3);
multiplyBy2Once(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the function &lt;strong&gt;&lt;em&gt;multiplyBy2Once&lt;/em&gt;&lt;/strong&gt; will be executed only one time and nevermore. We "edit" our function &lt;strong&gt;&lt;em&gt;multiplyBy2&lt;/em&gt;&lt;/strong&gt;, giving to it a new behavior. &lt;br&gt;
However, we don't need to change its original code, maintaining our function reusable.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works ⚙️
&lt;/h3&gt;

&lt;p&gt;Let's understand step by step in the code above how closures work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We stored one &lt;em&gt;const&lt;/em&gt; called &lt;strong&gt;&lt;em&gt;multiplyBy2&lt;/em&gt;&lt;/strong&gt; in the global memory, and its value its one function that receives one parameter called &lt;em&gt;number&lt;/em&gt; and returns &lt;em&gt;number * 2&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We stored one &lt;em&gt;const&lt;/em&gt; called &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt; in the global memory, and its value its one function that receives one parameter called &lt;em&gt;fn&lt;/em&gt; and returns one &lt;em&gt;const&lt;/em&gt; called &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We declared one &lt;em&gt;const&lt;/em&gt; called &lt;strong&gt;&lt;em&gt;multiplyBy2Once&lt;/em&gt;&lt;/strong&gt;, and its value we don't know yet because now we need execute the function oncefy first to know what it returns and atribute the return as the value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the moment that &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt; its executed, the interpreter creates a new execution context for this function. &lt;br&gt;
The first thing is take all function parameters (in this case &lt;strong&gt;&lt;em&gt;multiplyBy2&lt;/em&gt;&lt;/strong&gt;) and stores in the local memory of this context.&lt;br&gt;
Now the local memory have one &lt;em&gt;const&lt;/em&gt; called &lt;strong&gt;&lt;em&gt;fn&lt;/em&gt;&lt;/strong&gt; that have &lt;strong&gt;&lt;em&gt;multiplyBy2&lt;/em&gt;&lt;/strong&gt; function as value.&lt;br&gt;
Next step it's take all declarations inside the function and stores in the local memory (in this case, one &lt;em&gt;let&lt;/em&gt; &lt;strong&gt;&lt;em&gt;counter&lt;/em&gt;&lt;/strong&gt; with value 0 and one &lt;em&gt;const&lt;/em&gt; &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt; with value as one function that receives one parameter called &lt;strong&gt;&lt;em&gt;number&lt;/em&gt;&lt;/strong&gt;).&lt;br&gt;
After all declarations, it finally returns the &lt;em&gt;const&lt;/em&gt; &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When return our value &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt;, all the execution context (including the local memory) are removed, except the returned value, that now it's the value of the &lt;em&gt;const&lt;/em&gt; &lt;strong&gt;&lt;em&gt;multiplyBy2Once&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the next line of our code we execute the function &lt;strong&gt;&lt;em&gt;multiplyBy2Once&lt;/em&gt;&lt;/strong&gt; which, in the realite it's the function &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt; returned by &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A new execution context is created for our function and again the first thing is take all function parameters of this function and stores in the local memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now our local memory have one label called &lt;strong&gt;&lt;em&gt;number&lt;/em&gt;&lt;/strong&gt; and have the value the &lt;em&gt;argument&lt;/em&gt; 0&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's in this moment that things become interesting.&lt;br&gt;
In the next line of the execution, we have the condicional &lt;strong&gt;&lt;em&gt;if (counter === 0)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
So the interpreter goes to the function's local memory searching for the counter variable, but this variable doesn't exist in this execution context.&lt;/p&gt;

&lt;p&gt;It's now that closures make the magic.&lt;br&gt;
When a function is defined, it gets a bond to the surrounding local memory&lt;br&gt;
(“variable environment”) in which it has been defined.&lt;br&gt;
So, because the function &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt; was defined inside of function &lt;strong&gt;&lt;em&gt;oncefy&lt;/em&gt;&lt;/strong&gt;, the function &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt; "stores" all local memory of the context where that it's defined, including the &lt;em&gt;const&lt;/em&gt; &lt;strong&gt;&lt;em&gt;counter&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the interpreter doesn't find &lt;strong&gt;&lt;em&gt;counter&lt;/em&gt;&lt;/strong&gt; in the actual execution context scope, it goes up to the "backpack" that &lt;strong&gt;&lt;em&gt;myFunction&lt;/em&gt;&lt;/strong&gt; carries with itself (&lt;strong&gt;&lt;em&gt;aka closure&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;By keeping that memory, this context don't be erased, always remembering its last execution&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion 💡
&lt;/h3&gt;

&lt;p&gt;Closure it's a very important concept in programming and can be used for much things.&lt;/p&gt;

&lt;p&gt;Understand how it works may not be one easy task (and hard to explain too).&lt;/p&gt;

&lt;p&gt;However, it's important understand closures to understand more complex concepts and develop powerful solutions.&lt;/p&gt;

&lt;p&gt;See you soon!👋&lt;/p&gt;

&lt;p&gt;If you lost:&lt;br&gt;
&lt;a href="https://vequiato.dev/blog/understanding-classes-and-prototypes-5hhf/245484"&gt;Understanding classes and prototypes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://vequiato.dev/blog/understanding-higher-order-function-1dlg/250804"&gt;Understanding Higher Order Function&lt;/a&gt;  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding classes and prototypes</title>
      <dc:creator>João Guilherme do Amaral Vequiato</dc:creator>
      <pubDate>Tue, 21 Jan 2020 04:00:23 +0000</pubDate>
      <link>https://forem.com/jgamaraalv/understanding-classes-and-prototypes-5hhf</link>
      <guid>https://forem.com/jgamaraalv/understanding-classes-and-prototypes-5hhf</guid>
      <description>&lt;p&gt;In this article I'll try approach in a simple and objective way what I've learned (thanks to Will Sentance) about object orientend in Javascript, wich is a very popular program paradigm to structure complex codes.&lt;/p&gt;

&lt;p&gt;I'll initiate this with the principle of encapsulation, in how we can store data and their respective methods: &lt;strong&gt;Objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's start with the object vehicle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle1 = {
  type: "Car",
  name: "Belina",
  speed: 0,  
  accelerate: function() { vehicle.speed++; }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Supose we need now to create a new vehicile, let do this with a diferent ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the dot notation 🔨
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle2 = {};; // creates a empty object
// assigns the properties to the object
vehicle.type = "Car";
vehicle.name = "Monza";
vehicle.speed = 0;
vehicle.accelerate = function() {
  vehicle2.speed++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Object.create() 🛠️
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle3 = Object.create(null);
// assigns the properties to the object
vehicle3.type = "Car";
vehicle3.name = "Uno";
vehicle3.speed = 0;
vehicle3.accelerate = function() {
  vehicle3.speed++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All right, every above ways be valids to create an object, however you may be feeling uncomfortable with the ways we're did that.&lt;/p&gt;

&lt;p&gt;We're break a important principle in programming: &lt;strong&gt;&lt;em&gt;Don't Repeat Yourself&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What we can do?&lt;/p&gt;

&lt;h3&gt;
  
  
  Factories! Generating objects with functions 🏭
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function vehicleCreator(type, name, speed) {
  const newVehicle = {};
  newVehicle.type = type;
  newVehicle.name = name;
  newVehicle.speed = 0;
  newVehicle.accelerate = function () {
    newVehicle.speed++;
  };
  return newVehicle;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get eliminate the code copy every time we need to create a new vehicle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle1 = vehicleCreator("Car", "Belina", 0);
const vehicle2 = vehicleCreator("Car", "Monza", 0);
vehicle1.accelerate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We solve some of our problems. However, every time we've created a new vehicle we created unnecessary copies in the application memory of the &lt;strong&gt;&lt;em&gt;accelerate&lt;/em&gt;&lt;/strong&gt; method. That's sucks!&lt;/p&gt;

&lt;h3&gt;
  
  
  Using prototype chain ⛓️
&lt;/h3&gt;

&lt;p&gt;What if instead of creating a new method copy on each new object, we store our methods in other local and create a link these methods in the object, in a way what the interpreter can check the prototype chain and find them.&lt;/p&gt;

&lt;p&gt;We can creat this link using &lt;strong&gt;&lt;em&gt;Object.create()&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function vehicleFunctionStore() {
  accelerate: function() { this.speed++; }
}

function vehicleCreator(type, name, speed) {
  // the Object.create() serve to do the link between the new object created and the object methods vehicleFunctionStore.
  const newVehicle = Object.create(vehicleFunctionStore);
  newVehicle.type = type;
  newVehicle.name = name;
  newVehicle.speed = 0;
  return newVehicle;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the way we created and used the methods of each object stay the same, however were not created unecessary copies of the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle1 = vehicleCreator("Car", "Belina", 0);
const vehicle2 = vehicleCreator("Car", "Monza", 0);
vehicle1.accelerate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, how this link with the objects works?&lt;/p&gt;

&lt;p&gt;All Javascript object has one property called &lt;strong&gt;&lt;em&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
This property links all Javascript objects to a another big one, the &lt;strong&gt;&lt;em&gt;Object.prototype&lt;/em&gt;&lt;/strong&gt; that contains a built-in methods.&lt;/p&gt;

&lt;p&gt;It's exactly through this property we can access our &lt;strong&gt;&lt;em&gt;accelerate&lt;/em&gt;&lt;/strong&gt; method linked by &lt;strong&gt;&lt;em&gt;Object.create()&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// When we execute the accelerate method, the interpreter goes to the object vehicle1 search for the method. However, this method doesn't exist in the object. 
So, the interpreter goes to the Object.Prototype to verifies if the method exists, and there can find it and execute it!

vehicle1.accelerate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! We solve one more problem. But the things still out of the pattern here.&lt;br&gt;
This approach is useful and works, but exists another ways to facilitate our job.&lt;/p&gt;
&lt;h3&gt;
  
  
  The "new" keyword 🆕
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Let's change this way
const vehicle1 = vehicleCreator("Car", "Belina", 0);
const vehicle2 = vehicleCreator("Car", "Monza", 0);

// To this
const vehicle1 = new vehicleCreator("Car", "Belina", 0);
const vehicle2 = new vehicleCreator("Car", "Monza", 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;However we need to do some adaptations in our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function vehicleCreator(type, name, speed) {
  this.type = type;
  this.name = name;
  this.speed = speed;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice now we don't have to create a new object neither return one.&lt;br&gt;
The &lt;strong&gt;&lt;em&gt;new&lt;/em&gt;&lt;/strong&gt; keywords make do this for us! And is the only thing that word do.&lt;/p&gt;

&lt;p&gt;Now we've to create our method's link in the &lt;strong&gt;&lt;em&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/em&gt;&lt;/strong&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vehicleCreator.prototype.accelerate = function() {
  this.speed++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code have the same function what we did with de &lt;strong&gt;&lt;em&gt;Object.create()&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
Now we can create our objects with this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vehicle1 = new vehicleCreator("Car", "Belina", 0);
const vehicle2 = new vehicleCreator("Car", "Monza", 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All sweet, we facilitate so much the writing, performance and understanding of our code, but imagine that each new method we need to create a new link in prototype chain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vehicleCreator.prototype.accelerate = function() {
  this.speed++;
}
vehicleCreator.prototype.decelerate = function() {
  this.speed--;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to write our methods separeted of our object constructor. Sounds me unusual.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript classes 🎩
&lt;/h3&gt;

&lt;p&gt;From the ES2015 we had the insertion of syntactic sugar word &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
This emerges like a new development pattern and looks like with other programming languages.&lt;/p&gt;

&lt;p&gt;However, the &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt; keyword, under the hood, make exactly the same thing that we had above in the &lt;strong&gt;&lt;em&gt;The "new" keyword&lt;/em&gt;&lt;/strong&gt; topic.&lt;/p&gt;

&lt;p&gt;With it, we can declare our methods together with our object constructor and we don't need to explicitly link with prototype chain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class VehicleCreator {
  constructor(type, name, speed) {
    this.type = type;
    this.name = name;
    this.speed = speed;
  }
  accelerate() {
    this.speed++;
  }
  decelerate() {
    this.speed--;
  }
}
const vehicle1 = new vehicleCreator("Car", "Belina", 0);
vehicle1.accelerate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code above, under the hood, make exactly the same thing of the code below, but is more elegant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function vehicleCreator(type, name, speed) {
  this.type = type;
  this.name = name;
  this.speed = speed;
}
vehicleCreator.prototype.accelerate = function() {
  this.speed++;
}
vehicleCreator.prototype.decelerate = function() {
  this.speed--;
}
const vehicle1 = new vehicleCreator("Car", "Belina", 0);
vehicle1.accelerate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm end my article here. I've tried - and hope that I have succeeded - explain succinctly and direct about &lt;strong&gt;&lt;em&gt;prototype chain&lt;/em&gt;&lt;/strong&gt;, the diference between &lt;strong&gt;&lt;em&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/em&gt;&lt;/strong&gt; property and &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt; (the big object), and the way what &lt;strong&gt;&lt;em&gt;new&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt; keywords help us to object creation.&lt;/p&gt;

&lt;p&gt;See you soon!👋&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vequiato.dev/blog/understanding-closures-h2b/245969"&gt;Understanding closures&lt;/a&gt;&lt;br&gt;
&lt;a href="https://vequiato.dev/blog/understanding-higher-order-function-1dlg/250804"&gt;Understanding Higher Order Function&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
