<?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: Chandelier Axel</title>
    <description>The latest articles on DEV Community by Chandelier Axel (@chandelieraxel).</description>
    <link>https://dev.to/chandelieraxel</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%2F482747%2F7ae8da93-a18c-4910-b8ab-9dae8f4c8c34.jpeg</url>
      <title>DEV Community: Chandelier Axel</title>
      <link>https://dev.to/chandelieraxel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chandelieraxel"/>
    <language>en</language>
    <item>
      <title>Everything is a file in UNIX ?</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Sat, 11 Jun 2022 21:20:57 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/what-is-a-file-in-unix-systems--m5g</link>
      <guid>https://dev.to/chandelieraxel/what-is-a-file-in-unix-systems--m5g</guid>
      <description>&lt;p&gt;You most likely heard that &lt;em&gt;everything in UNIX is a file&lt;/em&gt;, but it never really clicked for you. Or you get it, but couldn't find the technical details behind it. Or you have no clues about what this even means - &lt;em&gt;like me before this&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In any cases, you are at the right place. Got your coffee ? ☕ Let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Is everything really a file ?
&lt;/li&gt;
&lt;li&gt;  What is a file, if everything is a file ?
&lt;/li&gt;
&lt;li&gt;  Inode

&lt;ul&gt;
&lt;li&gt;  Basics
&lt;/li&gt;
&lt;li&gt;  Inode number
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;  Directories
&lt;/li&gt;
&lt;li&gt;  Data
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Is everything really a file ?
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yup.&lt;/p&gt;

&lt;p&gt;This yet simple but quite complex sentence resume the entire architecture of the UNIX Operating System (&lt;strong&gt;OS&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Basically, every features your OS is able to do, rely on files. Processes, devices (such as a printer, or your mouse), networks, and even directories.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wait, directories ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yup. There are 7 different types of files, you can find a complete list &lt;a href="https://en.wikipedia.org/wiki/Unix_file_types"&gt;here&lt;/a&gt;. For now, we'll focus on regular files, and directories.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a file, if everything is a file ?
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From an OS point of view, a &lt;em&gt;file&lt;/em&gt; is actually nothing more than a specific data structure called an &lt;strong&gt;inode&lt;/strong&gt;. You may visualize an inode such as a table containaing file related informations.&lt;/p&gt;

&lt;p&gt;The inode itself contain all the metadatas (extra datas that describes data) relatives to the file it describe. Informations such as the creation date, the last update date, the ownership, the permissions, the file size ...&lt;/p&gt;




&lt;p&gt;⚠ &lt;em&gt;The file name and the actual file data are not part of the inode, keep it in mind, we'll cover this later.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Inode
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Basics
&lt;/h4&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can visualize an inode informations directly in your terminal, by typing the &lt;code&gt;ls -l&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Here's the kind of output you should receive :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 user  root  13 10 mai 22:10 index.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;File type and permissions. It's quite hard to read, let's break it down together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The first character specify the &lt;em&gt;what kind&lt;/em&gt; of file this is (the 7 types we mentionned earlier).

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;-&lt;/strong&gt; Regular file.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;d&lt;/strong&gt; Directory.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;l&lt;/strong&gt; Symbolic link.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;b&lt;/strong&gt; Block special file.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;c&lt;/strong&gt; Character special file.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;s&lt;/strong&gt; Socket link.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;p&lt;/strong&gt; FIFO.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;  The next three characters are related to the &lt;em&gt;owner&lt;/em&gt; permissions for the file.&lt;/li&gt;
&lt;li&gt;  The next three characters are related to the &lt;em&gt;group&lt;/em&gt; permissions for the file.&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The last three characters are related to the &lt;em&gt;others&lt;/em&gt; permissions for the file.&lt;/p&gt;

&lt;p&gt;All permissions fields can be read as follow : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the &lt;em&gt;permission&lt;/em&gt; allowed to &lt;strong&gt;read&lt;/strong&gt; the file ? &lt;strong&gt;-&lt;/strong&gt; for no, &lt;strong&gt;r&lt;/strong&gt; for yes. &lt;/li&gt;
&lt;li&gt;Is the &lt;em&gt;permission&lt;/em&gt; allowed to &lt;strong&gt;write&lt;/strong&gt; the file ? &lt;strong&gt;-&lt;/strong&gt; for no, &lt;strong&gt;w&lt;/strong&gt; for yes. &lt;/li&gt;
&lt;li&gt;Is the &lt;em&gt;permission&lt;/em&gt; allowed to &lt;strong&gt;execute&lt;/strong&gt; the file ? &lt;strong&gt;-&lt;/strong&gt; for no, &lt;strong&gt;x&lt;/strong&gt; for yes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number of hard links. You may find more informations &lt;a href="https://www.geeksforgeeks.org/soft-hard-links-unixlinux/#:~:text=A%20link%20in%20UNIX%20is,to%20the%20same%20file%2C%20elsewhere."&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Owner name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Group name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number of bytes in your file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Date of last modification.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;del&gt;File name&lt;/del&gt;. &lt;em&gt;Not a part of the inode, but still in the output. More on it later.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;📌 &lt;em&gt;You may find all these informations - and more, directly from your terminal in an UNIX environnement with the command &lt;code&gt;man ls&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;If you're willing to get a bit more informations about a specific file, you may want to use the &lt;code&gt;stat&lt;/code&gt; command directly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Inode number
&lt;/h4&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you execute the &lt;code&gt;ls -li&lt;/code&gt; command you will find an extra column at the beginning of the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;12345678 &lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 user  root  13 10 mai 22:10 index.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It contain a very specific integer, the &lt;strong&gt;inode number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;This number is the unique identifier for this specific file in your entire file system.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every files will have a different inode number (with the exception of &lt;a href="https://www.geeksforgeeks.org/soft-hard-links-unixlinux/"&gt;hard links&lt;/a&gt;), and there's a maximum amount of inode number your file system can handle. Afterward, you will not be able to create anymore files.&lt;/p&gt;

&lt;h3&gt;
  
  
  File name and directories
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the filename is not specified by the inode, where is it stored ?&lt;/p&gt;

&lt;p&gt;You most likely already guessed it by the title, but the file name is stored within the &lt;strong&gt;parent directory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What exactly is a directory then ? It's actually a pretty simple thing. Type &lt;code&gt;ls -l&lt;/code&gt; command somewhere with a directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;drw-r--r--  1 user  root  13 10 mai 23:17 directoryName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Noticed the &lt;strong&gt;d&lt;/strong&gt; at the beginning ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The directories files actually only contain a mapping table, between a file inode number, and his name.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Inode number&lt;/th&gt;
&lt;th&gt;File name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;900&lt;/td&gt;
&lt;td&gt;file.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;901&lt;/td&gt;
&lt;td&gt;data.json&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The list goes on for all the file or other directories it may contain.&lt;/p&gt;

&lt;p&gt;A directory is nothing but a specific file, it also have an inode number, and his name is saved within his parent directory inode.&lt;/p&gt;




&lt;p&gt;📌 &lt;em&gt;&lt;code&gt;ls -li&lt;/code&gt; for a directory to see the inode number&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;899 drw-r--r--  1 user  root  13 10 mai 23:17 directoryName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a complete representation of the content of a directory.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;The first two entries are references to itself, and to his parent directory.&lt;/strong&gt;&lt;br&gt;
📌 &lt;em&gt;To see them, use the &lt;code&gt;-a&lt;/code&gt; option on the &lt;code&gt;ls&lt;/code&gt; command to see hidden files.&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Inode number&lt;/th&gt;
&lt;th&gt;File name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;898&lt;/td&gt;
&lt;td&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;899&lt;/td&gt;
&lt;td&gt;..&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;900&lt;/td&gt;
&lt;td&gt;file.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;901&lt;/td&gt;
&lt;td&gt;data.json&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;902&lt;/td&gt;
&lt;td&gt;newDirectory&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  The file data
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, where is the file data ?&lt;/p&gt;

&lt;p&gt;What make the inode so special is that it kept references (&lt;strong&gt;pointers&lt;/strong&gt;) toward the memory blocks that are actually containing the data in disk. By doing so, when we ask to open the file, it go through all of them and recover the informations needed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The pointers part is oversimplified for the sake of this article, indirect pointers are purposely omitted.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's get a visual recap for a &lt;em&gt;file&lt;/em&gt; inode.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File Inode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;File types and permissions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hard links&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Owner name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Group name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number of bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(A lot more metadata informations )&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pointer toward block n° 100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pointer toward block n° 101&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;I hope you enjoyed the read, and learned as much as I did. If you have any questions or comments, feel free to reach to me on &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or in the comments below. Have a nice day !&lt;/p&gt;

</description>
      <category>unix</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why do React need to be in scope for JSX ?</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Mon, 20 Sep 2021 20:00:07 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/why-do-react-need-to-be-in-scope-for-jsx-3hen</link>
      <guid>https://dev.to/chandelieraxel/why-do-react-need-to-be-in-scope-for-jsx-3hen</guid>
      <description>&lt;h2&gt;
  
  
  &lt;code&gt;React must be in scope when using JSX&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Quite an annoying error, right ? What does it even mean anyway, you simply wrote a pretty basic component, and nothing happen but this error.&lt;/p&gt;

&lt;p&gt;You don't know how to fix it ? Or you never bothered looking at why you need to do so ? Or maybe you heard that the versions 17+ of React dispense us from doing so, but still getting the error ?&lt;/p&gt;

&lt;p&gt;You're at the right place, we'll go through the why, up to the how. Feel free to skip any part with the table of contents below.&lt;/p&gt;

&lt;p&gt;Got your coffee ? ☕ Let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why are we getting this error ?&lt;/li&gt;
&lt;li&gt;How to fix ?&lt;/li&gt;
&lt;li&gt;React version 17 and beyond&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why are we getting this error ?
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, in order to understand why, you need to know how the &lt;strong&gt;JSX&lt;/strong&gt; synthax work. For a complete breakdown, feel free to read this &lt;a href="https://dev.to/chandelieraxel/how-does-react-work-under-the-hood-jsx-wise-44pd"&gt;previous blog post&lt;/a&gt; that I wrote.&lt;/p&gt;

&lt;p&gt;For a quick answer, let's analyse a React component :&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CustomButton&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me !&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;CustomButton&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This example come directly from the React &lt;a href="https://reactjs.org/docs/jsx-in-depth.html#:~:text=React%20Must%20Be%20in%20Scope,scope%20from%20your%20JSX%20code.&amp;amp;text=If%20you%20don't%20use,scope%20as%20the%20React%20global."&gt;official documentation&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When React receive this component, it basically transform into 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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CustomButton&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because JSX is nothing but &lt;strong&gt;syntactic sugar&lt;/strong&gt; for the &lt;em&gt;createElement&lt;/em&gt; function, the code above will be called when creating our component.&lt;br&gt;
But .. In the context of our file, we never imported React. What will happen ?&lt;/p&gt;

&lt;p&gt;Exactly : &lt;code&gt;React must be in scope when using JSX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we don't import it at the top of our file, the &lt;em&gt;React.createElement&lt;/em&gt; would crash, as &lt;em&gt;React&lt;/em&gt; would be undefined.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to fix ?
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As stated before, you need to import React within your file, in order for the script to resolve properly the &lt;em&gt;createElement&lt;/em&gt; function. From here, you have multiple choices :&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Feel free to refer this Dan Abramov &lt;a href="https://twitter.com/dan_abramov/status/1308739731551858689"&gt;tweet&lt;/a&gt; for more informations.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At the end, it's up to your preferences, there's no immediate downsides using one or the other.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wait, didn't you say that in version 17+ we don't need it anymore ?&lt;/em&gt; &lt;strong&gt;Indeed&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  React version 17 and beyond
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As of React v.17.0, we are now free from doing such import. If you want more informations, here's the &lt;a href="https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html"&gt;link for the official release notes&lt;/a&gt; from the React team.&lt;/p&gt;

&lt;p&gt;If you want the quick explanation, they added some code for &lt;em&gt;compilers&lt;/em&gt; (such as Babel) to plug in, and add the import themselves when compiling our JSX. Hot stuff, right ?&lt;/p&gt;

&lt;p&gt;But you might still get the error.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes, but it's not from React ! As we said before, most of the time, the projects use a linting tool such as &lt;a href="https://eslint.org/"&gt;Eslint&lt;/a&gt;, and some specific set of rules as been created for React. One of them enforce you to import React if it detect any JSX within the file.&lt;/p&gt;

&lt;p&gt;If you're using React v.17.0 and beyond, you can freely disable the rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"react/jsx-uses-react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"off"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"react/react-in-jsx-scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"off"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now remove all the&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Before your finished your coffee, you learned why we &lt;strong&gt;&lt;em&gt;needed&lt;/em&gt;&lt;/strong&gt; to import React with JSX.&lt;br&gt;
I hope you enjoyed the read, and learned as much as I did. If you have any questions or comments, feel free to reach to me on &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or in the comments below. Have a nice day !&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to handle your JWT in your applications ?</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Wed, 18 Aug 2021 19:19:25 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/how-to-handle-your-jwt-in-your-applications-23d9</link>
      <guid>https://dev.to/chandelieraxel/how-to-handle-your-jwt-in-your-applications-23d9</guid>
      <description>&lt;p&gt;This question is a sensitive subject all over the internet. Wherever you'll look at, peoples tend to be really dogmatic. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;- Don't store it in Local Storage !!! Don't you know about XSS attacks ?!&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Please stop believing that storing your JWT in an HttpOnly cookie is secure........ You're still exposed to XSRF attacks.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You get the idea.&lt;/p&gt;

&lt;p&gt;Long story short, I was looking for informations to build a robust authentication system myself. I did know nothing about the attacks quoted above, and of course, how to protect my application.&lt;/p&gt;

&lt;p&gt;I'll do my best to summarize what I learnt, what are the different techniques and their fallbacks. This article will also try to be as opinion free as possible.&lt;/p&gt;

&lt;p&gt;Without further ado, let's dive in.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is the problem ?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I'll purposely fly through what a JWT is, to focus on the security part. You may find information on their dedicated &lt;a href="https://jwt.io/" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because there's one.&lt;/p&gt;

&lt;p&gt;Let's assume that you're building a new website, and you're on the authentication part. After some research, you find out that the go-to (as time of writing this) is using a JWT, a &lt;strong&gt;J&lt;/strong&gt;son &lt;strong&gt;W&lt;/strong&gt;eb &lt;strong&gt;T&lt;/strong&gt;oken.&lt;/p&gt;

&lt;p&gt;A JWT is basically an encoded string that will contain some basic informations (anything you want). Your server will send it back to you when you'll do your login process, and your client will need to supply it on any further requests where authentication is needed in order to be accepted by the server.&lt;br&gt;
In short, a JWT is a way to identify your user as a legitimate and authenticated one towards your server.&lt;/p&gt;

&lt;p&gt;So .. If we need to supply the JWT on any further request that need authentication, where do we tore it ?&lt;/p&gt;

&lt;p&gt;This is where things get interesting.&lt;/p&gt;
&lt;h2&gt;
  
  
  Local Storage
&lt;/h2&gt;

&lt;p&gt;My first idea, like a lot of people I believe, was to store my newly obtained JWT in the browser Local Storage. Things would be as simple as :&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;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jwt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jwtYouReceive&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And whenever we need it back :&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;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jwt&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;Despite beign the easiest way to store our JWT, it turns out that this is, by far, the most insecure way.&lt;br&gt;
By nature, everything stored within the localStorage is accessible through Javascript code. It means that, if &lt;em&gt;somehow&lt;/em&gt; a hacker could execute some JS code in our website, he could steal the JWT, and any of his requests would be accepted as an authenticated user. A way to do so is through an &lt;strong&gt;XSS&lt;/strong&gt; attack.&lt;/p&gt;


&lt;h3&gt;
  
  
  XSS Attack
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Cross Site Scripting&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Basically, an XSS attack happen when some undesirable code is being executed within your website. This can be as gentle as a console.log, but could go as far as stealing informations, our JWT for example.&lt;/p&gt;

&lt;p&gt;Let's just take a very contrived example to understand it better.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bxztxnq37fg6m9uhhvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bxztxnq37fg6m9uhhvp.png" alt="Unsecure app schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty simple, right ? Now here's the catch, what is sent through the form is not being &lt;strong&gt;sanitized&lt;/strong&gt; (meaning any unsecured or unrelated part of the data is removed or escaped), and so an hacker could insert a harmful script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    I juste created an amazing blog post !! 
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;functionToReadYourJWTandSendItToMe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt; 
    Please, accept it !
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This get inserted into the database, and when the admin open is page to see the preview of the blog post, the script will be hidden and being executed, succesfully stealing the admin JWT !&lt;/p&gt;

&lt;p&gt;And if the admin accept the blog post, and it get displayed on the website homepage, the script will execute for every visitor that will open the page .. Stealing everyone JWT !&lt;/p&gt;

&lt;p&gt;Here's a recap :&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3mi3tdgh95joq15393b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3mi3tdgh95joq15393b.png" alt="Unsecured app with hacker"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Storing the JWT in localStorage &lt;strong&gt;without&lt;/strong&gt; the proper defences against XSS can be dramatic, this left the hacker with a potential large area of actions all over your website to try to find a breach.&lt;br&gt;
The developpers now have the responsability to check for every possible breach and be mindful when developing new features.&lt;/p&gt;

&lt;p&gt;There are ways to secure our app to XSS, such as sanitizing everything that would go into the database.&lt;/p&gt;

&lt;p&gt;An easy to implement, but somewhat risky solution.&lt;/p&gt;

&lt;p&gt;Second solution.&lt;/p&gt;
&lt;h2&gt;
  
  
  HttpOnly Cookie
&lt;/h2&gt;

&lt;p&gt;While digging further in order to find informations about localStorage, I've seen a lot of peoples recommending to store the JWT into an &lt;strong&gt;HttpOnly&lt;/strong&gt; Cookie. If you're not confident what a cookie is, feel free to go to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; documentation.&lt;/p&gt;

&lt;p&gt;Be mindful that the &lt;strong&gt;HttpOnly&lt;/strong&gt; part is the most important one. A cookie without the HttpOnly attribute could be read by some JS code, sending us back to the XSS problem.&lt;/p&gt;

&lt;p&gt;By applying the attribute, we restrict the use of this cookie for HTTP requests only, securing us completely from XSS.&lt;/p&gt;

&lt;p&gt;But .. We're now prone to XSRF attacks.&lt;/p&gt;


&lt;h3&gt;
  
  
  XSRF Attack
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Cross Site Request Forgery&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As the name &lt;em&gt;may&lt;/em&gt; imply, the goal of this attack is to create a request on a malicious website to be executed on the targeted website. Let's take a real world example to understand it better.&lt;/p&gt;

&lt;p&gt;You have your website open and you're logged in. Your JWT is securely stored into an HttpOnly cookie, meaning that every request you send to your server will automatically include the cookie, and so your JWT.&lt;/p&gt;

&lt;p&gt;As every application with a user account, you have the possibility to change some informations by filling a form. This will send to your server a request, it'll verify your JWT, and allow the changes.&lt;/p&gt;

&lt;p&gt;As you navigate to it, you received an email. You open a new tab, open the email and click on the link.&lt;/p&gt;

&lt;p&gt;☠️ The website you lend on have a script that execute as soon as you open the page. Prepared in advance, it execute a request on &lt;strong&gt;your&lt;/strong&gt; website. ☠️&lt;br&gt;
How ? Well, the hacker could have created an account, open the dev tools and saw what was the endpoint to your server.&lt;/p&gt;

&lt;p&gt;Basically the hacker send the same request as you would done, but he control the informations. Your username has been changed, your profile picture aswell .. Maybe even your password.&lt;/p&gt;

&lt;p&gt;The most amazing part about this attack is that the hacker doesn't have to recover the JWT, it's &lt;strong&gt;automatically&lt;/strong&gt; included within the HTTP request.&lt;/p&gt;



&lt;p&gt;There are ways to secure your website from such attacks, which we won't cover here, but most of them tend to be prone to .. XSS.&lt;/p&gt;

&lt;p&gt;Third solution.&lt;/p&gt;
&lt;h2&gt;
  
  
  Storing it into memory
&lt;/h2&gt;

&lt;p&gt;Maybe even a simplier solution than localStorage, the goal is fairly simple. You attribute the JWT to a variable, and make it available for your needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;...;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This variable is impossible to reach for a hacker, neither from an XSS or XSRF attacks.&lt;/p&gt;

&lt;p&gt;Such a simple solution as one serious downside : whenever your user will close your website, the next time he'll come back, he'll need to login again, creating a very poor user experience.&lt;/p&gt;

&lt;p&gt;Just like the other solutions, there are ways to mitigate the downsides.&lt;/p&gt;

&lt;h3&gt;
  
  
  Having a refresh_token
&lt;/h3&gt;

&lt;p&gt;When you request your initial JWT, the plan is to get an extra token, a &lt;strong&gt;refresh_token&lt;/strong&gt; token (which is basically a JWT that will live longer). This token will be saved in the browser within an HttpOnly cookie, aswell as on the server within a database. His goal is to keep the user login without him having to go through the login process everytime your JWT expire, such a process is called a &lt;strong&gt;silent refresh&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can actually use this behavior to &lt;em&gt;pretend&lt;/em&gt; the user session is being persisted. As the refresh_token is stored within the cookies, we can use it across sessions. When our website boot-up, we will trigger a call to a particular endpoint, this endpoint will return a JWT only if the refresh_token is still valid.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- How is this secure if the refresh_token is a JWT too ?&lt;/em&gt;&lt;br&gt;
The refresh_token will &lt;strong&gt;only&lt;/strong&gt; be used and &lt;strong&gt;accepted&lt;/strong&gt; in the particular endpoint that is dedicated to him. Trying to access the rest of the API with it will fail.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- But a hacker could use a XSRF, right ?&lt;/em&gt;&lt;br&gt;
Yes but he won't be able to see the JWT that is returned.&lt;/p&gt;

&lt;p&gt;This method leads to a lot of boilerplate and overhead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;None of the solutions above are bullet-proof, there's always a way for a brilliant attacker to get in. Some solutions are easier to implement, some require more setup but offer an arguably better overall "protection".&lt;/p&gt;

&lt;p&gt;Pick what suit you the best.&lt;/p&gt;

&lt;p&gt;I hope it helped you to understand this incredibly dense topic as much as I did writing this.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/" rel="noopener noreferrer"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>100daysofcode</category>
      <category>security</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Javascript Modules
</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Sat, 17 Jul 2021 21:42:06 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/javascript-modules-hhf</link>
      <guid>https://dev.to/chandelieraxel/javascript-modules-hhf</guid>
      <description>&lt;p&gt;Whats is a &lt;strong&gt;module&lt;/strong&gt; ?&lt;/p&gt;

&lt;p&gt;Basically modules are pieces of code that can be imported anywhere in your application. A module is really easily recognizable, as soon as you see the &lt;strong&gt;import&lt;/strong&gt; or &lt;strong&gt;export&lt;/strong&gt; keyword, you're dealing with a module.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So... pretty much everything is a module ?&lt;/li&gt;
&lt;li&gt;What about the &lt;em&gt;require&lt;/em&gt; keyword I've seen here and here ?&lt;/li&gt;
&lt;li&gt;How do I use them ?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll cover everything from this point onward.&lt;/p&gt;

&lt;p&gt;Grab a coffee, and let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  A bit of history
&lt;/h2&gt;

&lt;p&gt;As we can't really understand something without the knowledge of &lt;em&gt;why&lt;/em&gt; it exist, we'll take a quick historical detour.&lt;/p&gt;

&lt;p&gt;Waaaaay back in the days, nothing known as modules existed. But also, Javascript wasn't as widely used as it is today, it's only purpose was pretty much to add interactivity on your website.&lt;br&gt;
Time goes on, and the use-cases grown as well. Javascript is more and more used, up to a point where JS was well-know for his monstruous single page codebase.&lt;/p&gt;

&lt;p&gt;And nobody like 2000 lines long of code within a single file, right ?&lt;/p&gt;

&lt;p&gt;So we needed a way to be able to split our code for obvious reasons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modules&lt;/strong&gt; were born.&lt;/p&gt;
&lt;h3&gt;
  
  
  AMD, CommonJS, UMD
&lt;/h3&gt;

&lt;p&gt;Unfortunaly, no specifications managed to solve this problem, so the community started to build tools that would solve the problem. &lt;/p&gt;

&lt;p&gt;This is what &lt;em&gt;AMD&lt;/em&gt;, &lt;em&gt;Common JS&lt;/em&gt; and &lt;em&gt;UMD&lt;/em&gt; are, tools to &lt;strong&gt;enable modularization&lt;/strong&gt; within our projects.&lt;/p&gt;

&lt;p&gt;From this point onward, we were able to do 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="c1"&gt;// multiply.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;multiply&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Relative path to the file&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're now able to get the multiply function from any file by adding the correct require at the top level of your file.&lt;/p&gt;

&lt;p&gt;2000 lines long files era is over !&lt;/p&gt;

&lt;h3&gt;
  
  
  ES6, export and import
&lt;/h3&gt;

&lt;p&gt;Up to the ES6 release in 2015, we had to use such tools to split our codes and using modules. Now we got a &lt;strong&gt;standardized&lt;/strong&gt; syntax and now work without such tools !&lt;/p&gt;

&lt;p&gt;If we take back our previous example, this how it look like now :&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;// multiply.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.js&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;multiply&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Relative path to the file&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ES6 is here, we can now use const keyword 😉&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Welcome to the present, let's dive straight into the &lt;em&gt;ES6 modules&lt;/em&gt; !&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;I'll take functions as exemples because they are, in my opinion, the easiest way to understand modules. But you can do the exacte same behavior with all JS data types such as objects, arrays, etc.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Named exports/imports
&lt;/h3&gt;

&lt;p&gt;You can either export 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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&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;OR 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&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="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are actually working and complety valid, they actually are doing the exact same thing. So what's the difference ? Well, let's say we have multiples things that we want to export, we can either add export in front of every functions ...&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;c&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;d&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;OR we could group all the export in one place :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;d&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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 now use our functions in a different file :&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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;./multiply.js&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;Be very careful, the names of the values within the brackets &lt;strong&gt;must be the same as the exported ones&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;foo&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;--- Will fail because there's nothing named 'foo' exported from the file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;IMPORTANT NOTES&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Exporting this way will give you &lt;em&gt;references&lt;/em&gt; to the exported things, meaning that every modification on the value, will have an impact on all your imports.&lt;/p&gt;

&lt;p&gt;Quick 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bar&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="c1"&gt;// After 10 ms, we change the value of bar&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&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;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;foo&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;./foo.js&lt;/span&gt;&lt;span class="dl"&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;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--- { bar: false }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if the modification was caused by a function into a specific workflow ? You now have a global mutation, and lost track of the value of your object within your application. It may cause crashes and such terrible headaches debugging.&lt;/p&gt;




&lt;h3&gt;
  
  
  Default exports/imports
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding the &lt;strong&gt;as default&lt;/strong&gt; allow you to do the following :&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;import&lt;/span&gt; &lt;span class="nx"&gt;multiply&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- You're now free to remove the brackets&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, wait, before we move on, let's use the shortcut synthax for the export :&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;OR&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&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;Using a default exports has multiples consequences : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It no longer export a &lt;em&gt;reference&lt;/em&gt; but a value, it may be changed witout impacts on other imports&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You are free to name your imports variable however you want, it's no longer binded to the exported names&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;foo&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- You're still getting the multiply function, but it's renamed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can only do ONE default export per module&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;IMPORTANT NOTES&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There's actually a difference between&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&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="nx"&gt;multiply&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is way past the goal of this article. Besides, Jake Archibald did an amazing blog post that you can found &lt;a href="https://jakearchibald.com/2021/export-default-thing-vs-thing-as-default/"&gt;here&lt;/a&gt; that fully cover the subject.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fancy but useful features
&lt;/h2&gt;

&lt;p&gt;We covered the basics to get you started with the modules in JS, but there's some more syntax to know about imports/exports !&lt;/p&gt;

&lt;h3&gt;
  
  
  Mixing named and default exports/imports
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//multiply.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&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="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Work like a charm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Renaming
&lt;/h3&gt;

&lt;p&gt;You're free to rename you imports/exports within the declaration :&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;// multiply.js&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;multiply&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// index.js&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;foo&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Will give you the multiply function&lt;/span&gt;

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&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;// multiply.js&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;multiply&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// index.js&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;multiply&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;foo&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;./multiply.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This came become handy in case you have name conflicts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Import everything at once
&lt;/h3&gt;

&lt;p&gt;Let's say you have a pretty big list of exported things :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;d&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of importing them all one by one  :&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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;./foo.js&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;You're allowed to do 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="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;whateverYouLike&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;./foo.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;whateverYouLike&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;whateverYouLike&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&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'll wrap up here, I hope you enjoyed and learnt has much as I did writing this, feel free to reach me to discuss about it or if any mistakes were made.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using await in loop cause performance issues</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Tue, 18 May 2021 18:14:36 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/using-await-in-loop-cause-performance-issues-3amb</link>
      <guid>https://dev.to/chandelieraxel/using-await-in-loop-cause-performance-issues-3amb</guid>
      <description>&lt;p&gt;Recently, I learned the hard way that &lt;strong&gt;await&lt;/strong&gt; is not the solution to every promises.&lt;/p&gt;

&lt;p&gt;At work, I had to write a piece of code that was looping for a &lt;strong&gt;lots&lt;/strong&gt; of elements.&lt;/p&gt;

&lt;p&gt;Bascially, it was looping over hundreds of elements, and was doing an HTTP request for each in order to retrieve some vitals informations.&lt;/p&gt;

&lt;p&gt;It was something like that :&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;//...&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;datas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="k"&gt;for&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;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;elements&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&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="s1"&gt;https://pokeapi.co/api/v2/pokemon/ditto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;datas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Use datas ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a really simplified exemple using a free Pokemon API (We all have our favorite API's 🙈). &lt;br&gt;
I didn't notice myself that it was causing a performance issue, it first came as an Eslint error :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Unexpected `await` inside a loop.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🤔🤔🤔&lt;/p&gt;

&lt;p&gt;It was time to dig and to follow the &lt;a href="https://eslint.org/docs/rules/no-await-in-loop"&gt;documentation link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And just to make sure Eslint wasn't lying to me (You should trust him 100%), I did some testings ...&lt;/p&gt;

&lt;h2&gt;
  
  
  The test
&lt;/h2&gt;

&lt;p&gt;Going back to our previous exemple, but with some &lt;strong&gt;console.time&lt;/strong&gt; to evaluate the actual time it take for our loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axios&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;axios&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;elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchPokemons&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;datas&lt;/span&gt; &lt;span class="o"&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;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wrong way&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for&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;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;elements&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&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="s1"&gt;https://pokeapi.co/api/v2/pokemon/ditto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;datas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;timeEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wrong way&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;fetchPokemons&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the Node code sample I used, feel free to try it out yourself.&lt;/p&gt;

&lt;p&gt;It would be painful to make you guess how much time it took for our loop to finish, so here you go :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Between 12 and 13 seconds.&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;    Wrong way: 13.191s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doesn't sound &lt;em&gt;that&lt;/em&gt; bad for 45 HTTP calls, but let's see how it goes if we refactor as Eslint told us.&lt;/p&gt;

&lt;h2&gt;
  
  
  The refactor
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchPokemons&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;promises&lt;/span&gt; &lt;span class="o"&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;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nice way&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;for&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;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;elements&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;axios&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="s1"&gt;https://pokeapi.co/api/v2/pokemon/ditto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;promises&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promises&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;actualDatas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// We need an extra loop to extract the data, and not having the requests stuff&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;timeEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nice way&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;fetchPokemons&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So ... What happened ?&lt;/p&gt;

&lt;p&gt;Well, we basically removed the &lt;em&gt;await&lt;/em&gt;, and pushed all our &lt;em&gt;unresolved&lt;/em&gt; promises into an array. Then we simply await for all of them to resolved, and we extract the datas.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Isn't it the same thing ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well .. Not really. Before we dive into the explanation, could you take a quick guess on how much time it take for us to gather all the datas ?&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Between 0.8 and 1.5 seconds.&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;    Nice way: 1.448s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🤯&lt;/p&gt;

&lt;p&gt;Did we just cut down the time by &lt;strong&gt;10&lt;/strong&gt; ? Yes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;It's pretty simple, previously we were waiting for each requests to resolve before launching the next one : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch first&lt;/li&gt;
&lt;li&gt;Wait N seconds until it resolved&lt;/li&gt;
&lt;li&gt;Launch seconds&lt;/li&gt;
&lt;li&gt;Wait N seconds until it resolved&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time add up &lt;strong&gt;a lot&lt;/strong&gt; as we saw.&lt;/p&gt;

&lt;p&gt;Now, it look like this : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch first&lt;/li&gt;
&lt;li&gt;Launch second&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;Wait for what's left to resolve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You got it ? By the time we were looping and launching everything, some - if not most, promises have already resolved !&lt;/p&gt;

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

&lt;p&gt;Now you'll think twice before awaiting in a loop.&lt;/p&gt;

&lt;p&gt;If you are a bit lost and don't really get what was happening here, I wrote an &lt;a href="https://dev.to/spartakyste/the-promises-guide-i-would-have-loved-as-a-junior-developper-3621"&gt;article&lt;/a&gt; that cover all the Promises basics for Javascript.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>performance</category>
      <category>promises</category>
    </item>
    <item>
      <title>A complete introduction to Docker</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Tue, 27 Apr 2021 19:51:07 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/a-complete-introduction-to-docker-o6m</link>
      <guid>https://dev.to/chandelieraxel/a-complete-introduction-to-docker-o6m</guid>
      <description>&lt;p&gt;You saw that icon with a blue whale being compared to a cargo ship ? I'll assume in this article that it's probably as meaningless to you, as it was to me.&lt;/p&gt;

&lt;p&gt;If you're like me, and heard of Docker for quite a long time now without knowing what it does, stay here we'll cover everything you need to get you started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;First of all, let's start with the basics. To understand &lt;em&gt;what&lt;/em&gt; is Docker and &lt;em&gt;why&lt;/em&gt; Docker, we need a basic understanding how &lt;strong&gt;V&lt;/strong&gt;irtual &lt;strong&gt;M&lt;/strong&gt;achines works. To keep it simple, how is organized the thing that is keeping the websites alive ?&lt;/p&gt;

&lt;h3&gt;
  
  
  VM (Virtual Machine)
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Disclaimer : I'll assume that you had little to no experiences with VM's, feel free to skip this part.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The theory
&lt;/h4&gt;

&lt;p&gt;You finally ended to develop your amazing application, you got all the needs covered, all your tests are green and your client (maybe it's yourself ?) is satisfied. It's now time to launch your application to the public.&lt;/p&gt;

&lt;p&gt;As you developped your product on a computer, you need a computer that can keep your application alive 24/7, right ? Here's the part were you need to host your application on a &lt;strong&gt;server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A server is basically a physical big piece of hardware with all the components for the CPU, RAM, etc needed. On this server, supplied by the service provider you choose, they'll boot up severals &lt;strong&gt;Virtual Machines&lt;/strong&gt; including the one you decided to paid for.&lt;/p&gt;

&lt;h4&gt;
  
  
  The hotel comparison
&lt;/h4&gt;

&lt;p&gt;To represent it easily, I like to compare with an hotel.&lt;/p&gt;

&lt;p&gt;This hotel is well-knowned for his themes based rooms (jungle, fairy, and so on).&lt;/p&gt;

&lt;p&gt;When the hotel was built, someone was choosen to manage everything, the ressources, the peoples that work here, the reservations etc. Let's call him/her the &lt;strong&gt;Hypervisor&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As soon as someone want to book a room, the Hypervisor will take the call and make it happen. You want a medium size room with a jungle theme ? No problem. A small one with a fairy universe for a children ? No problem. He'll allow the specific ressources from the hotel for your desires.&lt;/p&gt;

&lt;p&gt;This is exactly what's happening within a server. It possess an Hypervisor that will launch several Virtual Machines with specific configuration when needed. A medium room with the jungle theme is actually a Virtual Machine with an i5, 8GB RAM with a Linux OS.&lt;/p&gt;

&lt;p&gt;If you have been in the industry long enough, you would remember that back in the days, we had to pay for an entire server for ourselves, leading to a higher cost. We couldn't just get a piece of the server for our needs. To put that into the hotel comparison, we had to book the &lt;em&gt;entire&lt;/em&gt; hotel even if we only needed one room.&lt;/p&gt;

&lt;p&gt;Basically, VM's allow us to transform one server, into multiples servers. A single room hotel, from an hotel with multiples rooms.&lt;/p&gt;

&lt;p&gt;All hail to virtualization, right ?&lt;/p&gt;

&lt;h4&gt;
  
  
  The downsides
&lt;/h4&gt;

&lt;p&gt;Despite being a revolution compared to what we were doing before, the virtualization of the machines have a few downsides :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;They are slow to boot&lt;/em&gt; because of the entire &lt;strong&gt;O&lt;/strong&gt;perating &lt;strong&gt;S&lt;/strong&gt;ystem they are carrying and need to install from scratch.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;They are heavy&lt;/em&gt;, for the same reason explained above. They can take &lt;strong&gt;up to 10 GB&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Your application is bound to one OS&lt;/em&gt;, or you'll need multiples VM's. Imagine your application is divided into multiple services that have different requierements. You will most likely end into compatibilitiy issues. To solve that, you would have to either find a version that work for all, or boot another VM's with a different OS version and start the needed service here.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Docker solve this ?
&lt;/h3&gt;

&lt;p&gt;Docker enable you to pack your application into what they call a &lt;strong&gt;container&lt;/strong&gt;. But, what is a container ?&lt;/p&gt;

&lt;p&gt;A container is basically a lightweight, fast and portable piece of software. You can imagine it like a sealed bottle with a ship inside. No matter where you put it, no matter when, it's always gonna have the same look ... as long as it's not upside-down.&lt;/p&gt;

&lt;p&gt;We'll jump in the creation of the containers later, for now let's focus on the general comprehension.&lt;/p&gt;

&lt;p&gt;Alright, containers seems to be cool, but why use them ? Why Docker ?&lt;/p&gt;

&lt;p&gt;Well, as we saw earlier, VM's are very heavy, they take a lot of time to boot up because they need to install their own &lt;strong&gt;Kernel and OS&lt;/strong&gt;. In our hotel example, it could be translated this way : You booked a new room ? Alright we'll gather everyone and build this one as you desire. This includes peoples, materials, electricity, water .. And so on.&lt;/p&gt;

&lt;p&gt;This is where Docker come in action. Instead of saying "Build everything !", Docker will come, kick off the Hypervisor and say "Alright, they want a jungle based room ? Let's juste take the premade we have here, clone it and put it in place".&lt;/p&gt;

&lt;p&gt;Seems magical, right ? As you could have guessed, unlike VM's you do not need an Hypervisor when using Docker. You'll basically have a server, with Linux (or any others, really) based system, install Docker on it and boot up as many rooms as you want .. The rooms being the famous containers.&lt;/p&gt;

&lt;p&gt;Why are they so lightweight ? Because all the containers will &lt;strong&gt;share the same underline kernel&lt;/strong&gt;, unlike VM's that need one kernel each times. This is litteraly gamebreaker and allow them be started within seconds, literally. The downside of sharing the same kernel is that you can only boot up Linux containers on a Linux based system, and only Windows containers on a Windows based system.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  The containers
&lt;/h3&gt;

&lt;p&gt;As we saw before, containers are basically the bread and butter of Docker, they insure your app into a very isolated system with it's very specific needs. &lt;/p&gt;

&lt;p&gt;How are they created ? How do I start a Docker container ?&lt;/p&gt;

&lt;p&gt;First, you need an image. We'll explain what an image is and how to create one later. For now, all you need to know is that an image is pretty much the mould that you'll use for your container.&lt;/p&gt;

&lt;p&gt;For now, we'll use already created images. Most of the biggest technologies hanging around have their official images, such as NodeJS, Postgree, MongoDB, Redis .. And so on. You can find all of them on the official Docker website : &lt;a href="https://hub.docker.com/search?q=&amp;amp;type=image"&gt;Docker Hub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's play around with one of them. &lt;/p&gt;

&lt;p&gt;First, let's download the image, your mould :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker pull hello-world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will go straight to Docker Hub and get the latest version available. Once it's downloaded, the image will be avaiblable on your computer as long as you don't delete it. (More on the images on the next chapter)&lt;/p&gt;

&lt;p&gt;Now that you got it, time to do something with it, right ? Let's create our first container :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run hello-world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see a text appearing in your console, congratulation, everything is working !&lt;/p&gt;

&lt;p&gt;To see all your containers, you can type :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker container &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command will gather all the informations you need : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The container ID (really useful when you want to manipulate a specific container).&lt;/li&gt;
&lt;li&gt;His name (randomly generated by Docker, or you can choose your own).&lt;/li&gt;
&lt;li&gt;How much time passed since it's creation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And way more !&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This was a very basic example, the container is pretty much doing nothing. The purpose was to give you a quick insight on how we create containers.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For a more concrete exemple with a MongoDB database, I would recommend you &lt;a href="https://docs.docker.com/language/nodejs/develop/"&gt;this official tutorial&lt;/a&gt; by the Docker team.&lt;/p&gt;

&lt;h3&gt;
  
  
  The images
&lt;/h3&gt;

&lt;p&gt;We just created a container, based on an image. What is an image then ?&lt;/p&gt;

&lt;p&gt;Well, it's pretty easy. An image is a mould, a description, a package with instructions on how &lt;br&gt;
to create containers. They are created from a &lt;strong&gt;.dockerfile&lt;/strong&gt; with a set of instructions on how to be built.&lt;/p&gt;

&lt;p&gt;You can create your own image based on another one, everything is possible. You want to add a custom software on a Linux distribution ? Just get the image, write your .dockerfile and publish it on the Docker hub so everyone can get it !&lt;/p&gt;

&lt;p&gt;That's all, really.&lt;/p&gt;




&lt;p&gt;I hope you enjoyed this quick introduction into the Docker world. This was a very high-level overview, this is a very complex and a whole ecosystem in itself, there's plenty more to discover !&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
      <category>docker</category>
      <category>beginners</category>
      <category>devops</category>
    </item>
    <item>
      <title>My experience from bootcamper to freelancer.</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Tue, 02 Mar 2021 14:11:13 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/my-experience-from-bootcamper-to-freelancer-55l5</link>
      <guid>https://dev.to/chandelieraxel/my-experience-from-bootcamper-to-freelancer-55l5</guid>
      <description>&lt;p&gt;Today's article will be a bit different, I would like to retire from the code itself for a moment, and talk about my early developer experience. I have a feeling that people who started their career very recently might find this article helpful.&lt;/p&gt;

&lt;p&gt;I'll try to gather what were the most useful skills and things I've accomplished that allowed me to find my first job as developer, 2 months after a bootcamp, and then become a freelance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;My journey&lt;/li&gt;
&lt;li&gt;Personnal projects&lt;/li&gt;
&lt;li&gt;Tutorials&lt;/li&gt;
&lt;li&gt;Documentations&lt;/li&gt;
&lt;li&gt;Ask questions&lt;/li&gt;
&lt;li&gt;Share success&lt;/li&gt;
&lt;li&gt;Group activities&lt;/li&gt;
&lt;li&gt;Be focused&lt;/li&gt;
&lt;li&gt;Learn tests&lt;/li&gt;
&lt;li&gt;Tech challenges&lt;/li&gt;
&lt;li&gt;Teach&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My journey
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My coding journey started in a bootcamp in 2019, it was an intense 5 months of learning pretty much everything I needed to be a &lt;del&gt;successful&lt;/del&gt; developer.&lt;br&gt;
I had never touched nor seen an HTML tag whatsoever, but I had a feeling I would love it. I had no background in tech, no engineer degree and had nobody that could help or teach me.&lt;/p&gt;

&lt;p&gt;To give you an idea of what the program was, we had 5 months to learn the basics of : HTML, CSS (some CSS framework too, like Bootstrap), JS, React, Node and SQL.&lt;/p&gt;

&lt;p&gt;Needless to say it was a tough rush.&lt;/p&gt;

&lt;p&gt;Needless to say, if I had only been following the program without investigating and working on the side, I wouldn't have been able to find any job afterward (not that quickly, at least).&lt;/p&gt;

&lt;p&gt;I knew that we (my coworkers and I) would all have the same knowledge, and pretty much the same background in the end. It was a necessity to step out to be the chosen one.&lt;/p&gt;

&lt;p&gt;I had to &lt;em&gt;level up&lt;/em&gt; faster.&lt;/p&gt;

&lt;p&gt;Now, I've created my own company, &lt;a href="https://othrys.dev"&gt;Othrys&lt;/a&gt; with my partner &lt;a href="https://dev.to/gaelferrand"&gt;Ferrand Gaël&lt;/a&gt;. We're working as freelancers with companies such as AirBnB and the french store chain Monoprix.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'll go through what helped me. It might, and probably will, be different for you. Keep in mind that the goal is to help people that are learning. If you have any extra recommendations, leave it as a comment, I would be very happy editing the article and crediting you for it.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Make personal projects
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the most time-efficient thing you could ever do. Take a subject you haven't really understood yet, and build something with it. You'll stumble, but you'll learn &lt;strong&gt;a lot&lt;/strong&gt; from it. And this is something you'll be able to show to a potential recruiter to show your determination and what you can do. It will also give you confidence into your skills, and allow you to measure your progression when you go back into old projects.&lt;/p&gt;

&lt;p&gt;If you're wondering what to build, there are plenty of possibilities. Take a topic you love : a book, a video game, a movie, a sport, and make something you wish already existed, or build it better. Or find a very well done website, and try to mimic it.&lt;/p&gt;

&lt;p&gt;If you're really out of ideas, I would suggest you &lt;a href="https://dev.to/commentme/some-projects-to-build-your-resume-2leg"&gt;this article&lt;/a&gt; wrote by &lt;strong&gt;Unnati Bamania&lt;/strong&gt; that gathers a lot of possibilities.&lt;/p&gt;

&lt;p&gt;I personnaly created a competition system for one of my favorite video games using React, Node and an SQL database during my bootcamp. It took me a looong time, but I knew it could be useful as a proof of my abilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop following tutorials blindfolded
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of spending time copy/pasting a tutorial, try to find the solution by yourself. Doing so, you'll get a deeper understanding on what you're working on, and you'll be more prone to doing it again.&lt;/p&gt;

&lt;p&gt;If you're building something new, go read the documentation, it should be good enough to get you started with the basics.&lt;/p&gt;

&lt;p&gt;Don't get me wrong, I'm not saying to never read any tutorial. Some are very well done and well explained. If you're following one, you should focus on the explanations instead of stopping to the code samples and wondering why it's not working.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We all copy pasted that weird Stack Overflow function that does some unknow magic.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;It's working ... Right ?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;## Read the documentations
&lt;a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're digging through a tutorial, you might land on the documentation page of the technology you're using.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Especially when you're learning everything at once or in short period of time, it'll help wrapping your mind around the &lt;em&gt;why&lt;/em&gt;. It might be boring, and you will understand only a very tiny part from it at the beginning, but believe me, it'll pay off big time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ask dumb questions
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you keep reading resources here and here, you'll necessarily ask yourself questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ask them&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the only way to know for sure if you understood correctly. It might even be the genius moment when you ask the question, and everything becomes much more clear as soon as your formulate it.&lt;/p&gt;

&lt;p&gt;If you are lacking on people to reach, try social medias. Most of the developers I know are always happy to share a bit of time or knowledge with you. If not, they might even ask someone they know to help you out !&lt;/p&gt;

&lt;p&gt;Don't get discouraged if they don't, building a community take times.&lt;/p&gt;

&lt;p&gt;As a last resort, Google might be able to link to very good resources that could help you !&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you have specific code questions, you could even try to post on Stack Overflow.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Share your success
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should always be proud of what you accomplished, especially when you were stuck for hours/days on the problem. Other people will always be super happy to celebrate with you, they could also learn from your struggle, killing two birds with one stone.&lt;/p&gt;

&lt;p&gt;The same peoples that you'll be celebrating with, will cheer you up when that impostor syndrom will kick your ass off.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I would suggest you &lt;a href="https://epicreact.dev/podcast/taking-control-over-your-career"&gt;this podcast&lt;/a&gt; from Kent C Doods that cover this subject from his point of view.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prioritize group activities
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The time where a developer was that nerd boy deep down its basement is over, sorry. In every single company you'll be working in, you'll &lt;strong&gt;always&lt;/strong&gt; be working with other people. You must be able to cooperate with a group of people, to share your ideas with them, to ask questions when you need to or even to give some advice !&lt;/p&gt;

&lt;h2&gt;
  
  
  Be focused
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's a ton of things to discover and learn. It can be mindblowing, and if you are like me, you'll want to know eveything ... It's impossible and I consider this as a bad practice for beginners : You'll necessarily end up to know a tiny bit of everything and never be good enough at something in particular. &lt;/p&gt;

&lt;p&gt;Not exactly something very valuable when looking for your first job, right ?&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn how to write tests
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tests are the best way to ensure code quality, to chase bugs and make sure they'll never come back. A 100% tested application doesn't mean a bug-free one, but you'll know for sure that the bug you fixed a week ago will never come back.&lt;/p&gt;

&lt;p&gt;There's a whole subject about testing in general, this is an entire world with a tons of tools. It can be tricky aswell, but it will pay off big times when looking for a job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't be affraid to accept technical challenges
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're feeling it, you should volunteered to take a task you know very little about. Taking a subject you know very little to nothing about is one of the best way to progress.&lt;/p&gt;

&lt;p&gt;In my previous job, I personnaly had to learn Apollo, GraphQL and Typescript on the fly for a mission I was assigned. I'm not gonna lie, it was hard and that impostor syndrom kicked my ass off. It took weeks before I became confident with the techonologies and finally considered myself as a real developper for the mission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Teach
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only way to know for sure that you'll understand a topic, is to teach it to someone. If you can explain clearly a subject to someone else, you can be confident into your knowledge. Of course don't fool yourself and work on the subject before explaining.&lt;/p&gt;

&lt;p&gt;Another way to do so would be to write articles. You don't necessarily need to publish them, but the writing process will help you to discover where you knowledge is lacking.&lt;/p&gt;

&lt;p&gt;This is personnlay my favorite, I wrote one for &lt;a href="https://othrys.dev/the-npm-guide-i-would-have-loved-as-a-beginner/"&gt;NPM&lt;/a&gt; and one for &lt;a href="https://othrys.dev/promises/"&gt;Javascript promises&lt;/a&gt; in my process of relearning the basics.&lt;/p&gt;




&lt;p&gt;I hope you enjoyed the read, and if you made it that far, thank you !&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Promises guide I would have loved as a junior developper</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Tue, 09 Feb 2021 17:29:37 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/the-promises-guide-i-would-have-loved-as-a-junior-developper-3621</link>
      <guid>https://dev.to/chandelieraxel/the-promises-guide-i-would-have-loved-as-a-junior-developper-3621</guid>
      <description>&lt;p&gt;Do you remember stumbling on your first "Promise {pending}" ? Putting a .then just to see if it magically work ? This article is made for you, after our last article about &lt;a href="https://dev.to/spartakyste/the-npm-guide-i-would-have-loved-as-a-beginner-4i07"&gt;NPM&lt;/a&gt;, I'll take you with me in my process of re-learning the basics bricks of the JS ecosystem.&lt;/p&gt;

&lt;p&gt;The scope of this article is basically from understanding Promises, (how to write and recognize them), to an async/await dive. Fell free to skip to any parts with the table of contents below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer : We won't see the 'hidden' part of Promises. By hidden, I mean how the Javascript engine handle them inside the call stack.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Necessary background informations

&lt;ul&gt;
&lt;li&gt;So, what are Promises ?&lt;/li&gt;
&lt;li&gt;Why do we need them ?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Let's dig into the code

&lt;ul&gt;
&lt;li&gt;How can I use them ?&lt;/li&gt;
&lt;li&gt;How can I recognize a Promise I didn't write ?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Some additionnal informations

&lt;ul&gt;
&lt;li&gt;Async / Await&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Necessary background informations
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;The MDN web documentation&lt;/a&gt; definition for promises is the following :&lt;br&gt;
&lt;strong&gt;A Promise is a proxy for a value not necessarily known when the promise is created.&lt;br&gt;
It allows you to associate handlers with an asynchronous action's eventual success value or failure reason&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If it's unclear to you as it is for me, stay with me, we'll clear everything up, I &lt;em&gt;promise&lt;/em&gt; 😉.&lt;/p&gt;
&lt;h3&gt;
  
  
  So, what are Promises ?
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we let this bad joke behind, let's dive straigt into it.&lt;/p&gt;

&lt;p&gt;First, the basics. Javascript is a synchronous and mono-threaded language. It means that all your code will execute in the order it's write, and it only has one call stack. To keep it simple, we'll stand that JS is a language where eveything happen in order, without any external add-ons.&lt;/p&gt;

&lt;p&gt;Promises are a way to execute certain pieces of code &lt;em&gt;asynchronously&lt;/em&gt;, meaning they'll be executed behind the scenes, while the rest of the synchronous code is done.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why do we need them ?
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take a real life exemple with two waiters. To keep it simple, our waiters are responsible from taking the orders, and delivering the dishes froms the kitchen to the clients.&lt;/p&gt;

&lt;p&gt;Our first waiter will be the synchronous one (like if Javascript promises never existed). He would take the order, give it to the kitchen, &lt;strong&gt;&lt;em&gt;wait&lt;/em&gt;&lt;/strong&gt; for the dishes to be ready, and finally serve them, and so on. Kinda awkward and inefficient.&lt;/p&gt;

&lt;p&gt;Our second waiter will handle things asynchronously. He'll take the orders from the clients and give it to the kitchen. By the time the dishes are ready, he will go &lt;strong&gt;&lt;em&gt;do something else&lt;/em&gt;&lt;/strong&gt; and come back later for the dishes whenever they are ready.&lt;/p&gt;

&lt;p&gt;This is exactly what's happening in JS. The kitchen will give a &lt;em&gt;promise&lt;/em&gt; to the waiter that the dishes will be ready &lt;strong&gt;sometime&lt;/strong&gt; in the future.&lt;/p&gt;

&lt;p&gt;If promises never existed, all our code that requires an external HTTP call will block the rest from executing until the call is over, just like our first waiter was stuck at the kitchen in between orders.&lt;/p&gt;

&lt;p&gt;Concretely, on a website, you couldn't see the text or the shape until everyting has been loaded, leading to enormous loading time, or weird-looking pages. And nowadays, nobody want to wait more than 2 or 3 seconds for a website to load, right ?&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's dig into the code
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  How can I use them ?
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's write our first Promise. A very basic one would look 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="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;// Your asynchronous code&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A promise always take a function with two arguments : &lt;strong&gt;resolve&lt;/strong&gt; and &lt;strong&gt;reject&lt;/strong&gt;. When the promise must return the result, we call resolve with the results. If something wrong happened, let's say we're missing some ingredients, the whole promise is compromised, we must cancel the order and get something different from the client, this is where we call reject.&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;// Your promise can be stored in a variable, or returned within a function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;preparingDishes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isIngredientMissing&lt;/span&gt; &lt;span class="o"&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;dishes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;// Everything that the client ordered, it would be filled up as soon as one is ready&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="c1"&gt;// But if an ingredient is missing, immedialty call back the waiter to inform the clients&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isIngredientMissing&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;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;An ingredient is missing !&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// We use setTimeout to mimic an HTTP call&lt;/span&gt;
  &lt;span class="nx"&gt;setTimeout&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;// 10 seconds later, once the dishes are ready, send them to the waiter&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dishes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;10000&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;Now, what is the value of &lt;strong&gt;preparingDishes&lt;/strong&gt; ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;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;preparingDishes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;---- What will appear in the console ?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're already familiar with promises, you answered &lt;strong&gt;&lt;em&gt;"Promise {pending}"&lt;/em&gt;&lt;/strong&gt; and you're right.&lt;br&gt;
If you were expecting &lt;strong&gt;&lt;em&gt;[ ]&lt;/em&gt;&lt;/strong&gt; don't worry, we'll figure this out.&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="nx"&gt;preparingDishes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;---- Promise {pending}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why ? If we keep with our waiter exemple, we say to the kitchen : &lt;strong&gt;Hey, I have a new order, prepare it&lt;/strong&gt;, and we didn't let the necessary preparation time, so the kitchen answer &lt;strong&gt;It's not ready ! We're still preparing it&lt;/strong&gt;. The promise is &lt;em&gt;pending&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;How do we access the value then ?&lt;/p&gt;

&lt;p&gt;Do you remember the MDN definition ? &lt;strong&gt;It allows you to associate handlers&lt;/strong&gt;, the keywords here is handlers. Let's go back to our previous exemple, but let's get it to work for real this time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;preparingDishes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;// See the code above&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Now that our promise is created, let's trigger it, and then read the results&lt;/span&gt;
&lt;span class="nx"&gt;preparingDishes&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;dishes&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;// dishes is a arbitrary name, usually it's called result&lt;/span&gt;

    &lt;span class="cm"&gt;/* Within this function, we can access the result of the promise. The parameter will be the value you gave to the resolve.
    You are guaranted that anything you put in here, will execute when the promise is fullfilled (succesfull) */&lt;/span&gt;
    &lt;span class="nx"&gt;callWaiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dishes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// In case an error happened, this handler will catch the return value inside your above reject or any error that could happen in your promise code&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;An ingredient is missing !&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;sendWaiterBacktoClients&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="k"&gt;finally&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;// This one will execute anything that you put inside, either the promise succeed or not&lt;/span&gt;

    &lt;span class="c1"&gt;// For example, wether the kitchen succeed preparing the dishes or not, they'll have to start the next one&lt;/span&gt;
    &lt;span class="nx"&gt;prepareNextDishes&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 must have noticed by now, the &lt;em&gt;.then&lt;/em&gt;, &lt;em&gt;.catch&lt;/em&gt; and &lt;em&gt;.finally&lt;/em&gt; are the handlers MDN is talking about. Each will execute under different circumstances as stated above.&lt;br&gt;
Please take note that attaching all the handlers isn't mandatory, you could only attach a .then for exemple (but I wouldn't recommended it), or only a .then and a .catch, which is what you'll use most of the time.&lt;/p&gt;

&lt;p&gt;Nice ! Now that we write our first promise, and used it properly, let's go to the last part, where I personnaly struggled a lot with.&lt;/p&gt;
&lt;h3&gt;
  
  
  How can I recognize a Promise I didn't write ?
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say you're onboarding on a new project, you must get used to the codebase, but you're not sure what is asynchronous or not. Or even better, you're trying to figure out how to use a thrid-party library without the documentation.&lt;/p&gt;

&lt;p&gt;You have 2 solutions to figure if a specific piece of code is a promise or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you're using VSC as your text editor, you can let your mouse over the piece of code you're interested in. In the pop-up that's appearing, you can analyse what is the value. If this is a promise, you will see it as :
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Don't let the 'any' keyword instill doubt, this is some fancy Typescript, and will be replace with any value the promise is returning.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a &lt;em&gt;.then&lt;/em&gt; is hanging around, the variable before it is 100% a promise (unless the code you're stepping in is already broke 😕).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Additional informations
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we saw it, a promise is always followed by specific handlers. They are all &lt;strong&gt;meant&lt;/strong&gt; to be used with promises, using them with a string for example will lead to errors where JS will complain that &lt;em&gt;.then&lt;/em&gt; is not a function.&lt;/p&gt;

&lt;p&gt;Even if promises are very cool, nesting promises in promises can lead to what we call the &lt;strong&gt;callback hell&lt;/strong&gt;. Imagine that a specific piece of code result from a serie of promises, you'll have to wait the previous promises to be completed to access the value, and so on, leading to scary things 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;gettingOrder&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;order&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;giveOrderToKitchen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&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;dishes&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;serveClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dishes&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;startOver&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;I purposely omitted all the &lt;em&gt;.catch&lt;/em&gt; here, the readability already took a shoot. To solve this, we do have a powerful tool, &lt;strong&gt;async/await&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Async / Await
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, I would like to clarify something that took me a long time to understand, &lt;strong&gt;async/await is nothing more than syntactic sugar for promises&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's rewrite our first promise :&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;// Your function must be async to use the await keyword&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;waiterJob&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&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;dishes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;preparingDishes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&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;span class="c1"&gt;// Handle the 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, couple things changed here, we now have a function with the keyword &lt;em&gt;async&lt;/em&gt;, a &lt;em&gt;try/catch&lt;/em&gt; block, and the &lt;em&gt;await&lt;/em&gt; keyword. Even if we still don't get what happened here yet, we can already say that it's way cleaner.&lt;/p&gt;

&lt;p&gt;Here are the rules for using async/await :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The await keyword can &lt;strong&gt;only&lt;/strong&gt; be used within an async function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It &lt;strong&gt;replace&lt;/strong&gt; the .then, you &lt;strong&gt;must not&lt;/strong&gt; use it in conjuncution with .then/.catch. Exemple :
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// ☠️ Don't&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;preparingDishes&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;dishes&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;// Don't do this kind of no sense either, it would work, but it's way more readable as a full async/await&lt;/span&gt;
  &lt;span class="nx"&gt;preparingDishes&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="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dishes&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;something&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;ul&gt;
&lt;li&gt;Making a function async will enforce it's return value to be a promise. In our exemple the waiterJob function is a promise that you'll have to await aswell when calling it.&lt;/li&gt;
&lt;li&gt;Using await is a bit more tricky than the .then. If you want, you could await in front of a string, JS won't complain unlike the .then. Be very careful of not using it everywhere.

&lt;ul&gt;
&lt;li&gt;Using await where you don't need to won't lead to bug in itself, but the async function around it can, because it can break you app if you don't handle it properly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;To handle errors properly, you must wrap your promise call within a try/catch block. Our previous .catch will be handled here, and anything that break inside the try will be caught.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Hold on a second, you said that all async function must be awaited ? This is endless !&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, not really. Most of the time in your real apps, you'll have a synchronous function upward which nobody depends on, like an initialization function.&lt;/p&gt;

&lt;p&gt;If you don't have any, you could write what we call &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE"&gt;IIFE&lt;/a&gt;, it's basically a self-invoked function, allowing you to do 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="c1"&gt;// How can I achieve this ?&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;prepareTable&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;./&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;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prepareTable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;---- Error, it's not wrapped within an async function&lt;/span&gt;

&lt;span class="c1"&gt;// ----------------------------------------&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;prepareTable&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;./&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prepareTable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 👍&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what would look like our last example refactored with async/await&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;waiterJob&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&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;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;gettingOrder&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;dishes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;giveOrderToKitchen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&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;serveClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dishes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;startOver&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&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;span class="c1"&gt;// Handle the 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;We'll wrap up here, we saw everything that will help you start with promises. If you are a more experienced developper and you think something is missing, please feel free to add a comment for it.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>The NPM guide I would have loved as a beginner</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Thu, 10 Dec 2020 21:09:24 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/the-npm-guide-i-would-have-loved-as-a-beginner-4i07</link>
      <guid>https://dev.to/chandelieraxel/the-npm-guide-i-would-have-loved-as-a-beginner-4i07</guid>
      <description>&lt;p&gt;As a junior developer, I struggled a lot with NPM.&lt;br&gt;
Do you remember copy/pasting all the npm commands without knowing what they do ? Or the first time you freaked out while opening the package-lock.json ? What if I tell you that there's a &lt;em&gt;bugs&lt;/em&gt; property that you can set in your package.json ?&lt;/p&gt;

&lt;p&gt;No more fear is allowed from that point onward, we're going to learn together the basics of NPM.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer - The article will follow my process of re-learning from scracth. Feel free to skip to specific parts if you don't want to read what NPM means, etc.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A little background&lt;/li&gt;
&lt;li&gt;Initialization&lt;/li&gt;
&lt;li&gt;Manage dependencies&lt;/li&gt;
&lt;li&gt;Dependencies &amp;amp; DevDependencies&lt;/li&gt;
&lt;li&gt;
Side notes

&lt;ul&gt;
&lt;li&gt;Uninstall a library&lt;/li&gt;
&lt;li&gt;Install specifid version&lt;/li&gt;
&lt;li&gt;The package-lock&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  A little background
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start with the basics, what does NPM even mean ? It stand for &lt;strong&gt;Node Package Manager&lt;/strong&gt;, and as the name implies, it's responsible for managing your packages within your Node application.&lt;/p&gt;

&lt;p&gt;Now considered as a major piece of Javascript ecosystem, NPM offer an easy way to manage all the external dependencies we'll need to use in our project with a pretty simple command : &lt;strong&gt;npm install &lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I will skip the installation of NPM, the &lt;a href="https://nodejs.org/en/"&gt;Node website&lt;/a&gt; will explain it to you properly, and is not the core of this article.&lt;/p&gt;

&lt;p&gt;I'll jump straight to the npm commands. Let's start.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initialization
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I typed my first npm commands, I had absolutely no idea what was going on, despite being the core of NPM. Let's see it in detail.&lt;/p&gt;

&lt;p&gt;First of all, we need to create a node-based application, and this is as easy as running the following command.&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;npm&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll be prompted a few questions about our project, such as the project name, the link to your Git repository, ect. But for now, let's just skip and press Enter.&lt;/p&gt;

&lt;p&gt;Wait, use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing, we skipped all the questions.&lt;/p&gt;

&lt;p&gt;So, we now have a &lt;strong&gt;package.json&lt;/strong&gt; filled with some informations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your_directory_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file can be considered as the ID Card of our application. It contains its name, the current version, a tiny description, some keywords to help other people finding it, and a bunch of other stuff.&lt;/p&gt;

&lt;p&gt;(For a complete list of the properties, &lt;strong&gt;including the 'bugs' keyword we talked about&lt;/strong&gt;, please refer to the &lt;a href="https://docs.npmjs.com/cli/v6/configuring-npm/package-json#directories"&gt;official documentation&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You're also free to update it whenever you want, as long as you respect the JSON format and use the correct properties.&lt;/p&gt;

&lt;p&gt;Next, we want to start our application, right ?&lt;/p&gt;

&lt;h4&gt;
  
  
  Scripts
&lt;/h4&gt;

&lt;p&gt;Let's look at some examples. I'll assume you have worked at least once with either an Express-based application, or one of the popular JS front-end frameworks (such as React, Angular or Vue).&lt;/p&gt;

&lt;p&gt;This scripts property give you the power to customize npm commands to use within your application.&lt;/p&gt;

&lt;p&gt;Wait a second.. Is that the place were the &lt;strong&gt;npm start&lt;/strong&gt; I use everyday is defined ? Exactly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can specify any valid shells commands here, and create as much entries as you want nor need. You can even combine them !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"stuffA:"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"stuffB"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"together"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run stuffA &amp;amp; npm run stuffB"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Beware, this will run your scrips in parallel. To run concurrently, either replace the "&amp;amp;" by "&amp;amp;&amp;amp;", or look at &lt;a href="https://www.npmjs.com/package/concurrently"&gt;the Concurrently package&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Now, &lt;strong&gt;npm run together&lt;/strong&gt; ! And yes, this is not a typo, you need to write the &lt;em&gt;run&lt;/em&gt; keyword. In fact, even the npm start command is launched as &lt;strong&gt;npm run start&lt;/strong&gt; behind the scenes. (This ability is shared with a couple other keywords, such as install, test..)&lt;/p&gt;

&lt;p&gt;You still there ? Nice, let's keep digging and unleash the full power of NPM by adding some dependencies !&lt;/p&gt;

&lt;h2&gt;
  
  
  Manage dependencies
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nowadays, an npm package already exists for pretty much anything. It would be a shame not to use them, and to rebuild the wheel everytime.&lt;/p&gt;

&lt;p&gt;This is probably the biggest role of the package.json, it'll keep track of all the dependencies within your project, including the versions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install&lt;/span&gt; &amp;lt;package_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will download all the files needed and install them into a brand new &lt;em&gt;node_modules&lt;/em&gt; folder.&lt;br&gt;
This folder will become bigger and badder as you'll install more and more packages (and the packages themselves most likely depend on others packages, which will be installed too).&lt;/p&gt;

&lt;p&gt;Please don't do the same mistake as I did, and prevent committing this folder to your repository !&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;//.gitignore file at your project root&lt;/span&gt;

&lt;span class="nx"&gt;node_modules&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dependencies &amp;amp; DevDependencies
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, in your package.json ..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.17.1"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;lt;---&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;version&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"eslint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.13.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is this ? Actually, it's quite simple. Whenever you'll install something through the npm install command, it'll list it there. Doing so, when you'll share your amazing project with the world, the others devs will only launch &lt;em&gt;npm install&lt;/em&gt; and all the libraries required for your project to run, will install nicely.&lt;/p&gt;

&lt;p&gt;Now what are devDependencies ? Everything that is &lt;strong&gt;not&lt;/strong&gt; vital for your application and that should be removed from your production build will go there (such as your linter, for example). Be careful, you have to manage them yourself.&lt;/p&gt;

&lt;p&gt;By default, the npm install command will put everything inside the regular dependencies. To put something in the devDependencies, you must pass an extra argument to the command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; &amp;lt;your_library&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; &amp;lt;your_library&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR even shorter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm i &lt;span class="nt"&gt;-D&lt;/span&gt; &amp;lt;your_library&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Organizing your dependencies will lead to better production performance. You might not need your linter rules or your Typescript types definition to run your app, right ?&lt;/p&gt;

&lt;p&gt;Even better, npm allow us to omit the devDependencies on installation !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--only&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;prod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Side (but important) notes
&lt;/h3&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Uninstall a library
&lt;/h4&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Made a typo and forgot the --save-dev in your command ?&lt;br&gt;
Cleaning up your app from useless modules ?&lt;br&gt;
You have two options, either remove the package and reinstall it again with the proper command, or do it manually in your package.json.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm uninstall &amp;lt;your_library&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove the library from the package.json &lt;strong&gt;and&lt;/strong&gt; from the node modules.&lt;/p&gt;

&lt;p&gt;In case you want to remove it from the node modules but &lt;strong&gt;not&lt;/strong&gt; from the package.json (let's say the installation failed for whatever reason).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm uninstall &lt;span class="nt"&gt;--no-save&lt;/span&gt; &amp;lt;your_library&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you can't be bothered playing with the shell commands, you can also update manually your package.json.&lt;/p&gt;

&lt;p&gt;Let's get back our previous example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.17.1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"eslint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.13.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove the eslint dependency, erase it, and simply re-run npm install.&lt;/p&gt;

&lt;h4&gt;
  
  
  Install a specific version
&lt;/h4&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you'll have to install a specific version of a package. It's easy :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install&lt;/span&gt; &amp;lt;your_library&amp;gt;@&amp;lt;version&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install &lt;/span&gt;eslint@1.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The package-lock.json
&lt;/h4&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last but not least, the package-lock. It's actually here to solve a problem from the package.json we haven't talked about.&lt;/p&gt;

&lt;p&gt;We saw earlier that when installing a new package, his version is set into the package.json. It uses the &lt;strong&gt;semver convention&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Basically, the first character before the actual version number will implies some slight changes whenever the npm install command is ran.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~4.17.1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;~&lt;/strong&gt; mean that NPM will go and look for the &lt;strong&gt;4.17.1&lt;/strong&gt; version, but if a newer patch release is available, let's say &lt;strong&gt;4.17.9&lt;/strong&gt;, it'll use this one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.17.1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;^&lt;/strong&gt; mean that NPM will go and look for the &lt;strong&gt;4.17.1&lt;/strong&gt; version, but if a newer minor release is available, let's say &lt;strong&gt;4.18.1&lt;/strong&gt;, it'll use this one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"4.17.1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you omit a character, NPM will always use this exact version whatever happens.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you always specify the exact version, the following problem I'll explain is already out of sight&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let's say you have been working for a couple years on a project, and a new contributor clones and installs it.&lt;/p&gt;

&lt;p&gt;Since a lot of time passed, some of our dependencies must have received some new releases. As we explained earlier, depending on your semver convention, NPM will look for potential newer versions...&lt;/p&gt;

&lt;p&gt;And there we hit the wall, your project and the newly installed one are actually &lt;strong&gt;different&lt;/strong&gt; because some dependencies do not match the version specified in the package.json.&lt;/p&gt;

&lt;p&gt;Package-lock to the rescue. As his name implied, it'll &lt;strong&gt;lock&lt;/strong&gt; the version number in stone and will guarantee that the same package version is installed on every subsequent installation.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

</description>
      <category>npm</category>
      <category>javascript</category>
    </item>
    <item>
      <title>I'm going to convince you using React Hooks</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Fri, 16 Oct 2020 17:28:13 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/i-m-going-to-convice-you-using-react-hooks-461h</link>
      <guid>https://dev.to/chandelieraxel/i-m-going-to-convice-you-using-react-hooks-461h</guid>
      <description>&lt;p&gt;(If you are not using them yet, of course).&lt;/p&gt;

&lt;h1&gt;
  
  
  Quick intro
&lt;/h1&gt;

&lt;p&gt;Lately, I've been working on a React app as part of my job. We are working with the usual React + Redux, with the old "class" way (which are still completely valid, don't get me wrong). But you know, hooks have been around for quite a while, and they are amazing. Let me show you why they are, and give me a chance to convince you using them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Redux
&lt;/h4&gt;

&lt;p&gt;If you have worked with Redux before, you know the drill:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the store&lt;/li&gt;
&lt;li&gt;Wrap your application within the store&lt;/li&gt;
&lt;li&gt;Connect the component with the HOC (Higher-Order-Component)&lt;/li&gt;
&lt;li&gt;Create the mapStateToProps and mapDispatchToProps functions and applied theses to the connect ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pretty basic things that we do everyday, you may even have some VSC &lt;em&gt;snippets&lt;/em&gt; to help you out.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;Let's leave Redux for a second. How would you declare a state ?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create your component&lt;/li&gt;
&lt;li&gt;Call the constructor and the super&lt;/li&gt;
&lt;li&gt;Create your this.state variable and fill it with values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;~ Wait, in a real world exemple, there is more ...&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;~ You're right, let's add some lifecycles.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Lifecycles
&lt;/h4&gt;

&lt;p&gt;In every real apps, you will have to gather some datas from your API endpoints. The best place to do so is within the ComponentDidMount. Sometimes, you even need to apply things on certains updates, it's where ComponentDidUpdate is coming handy. And of course you need to clean your subscriptions into a ComponentWillUnmount ...&lt;/p&gt;

&lt;p&gt;A lot of things to remember, right ? And a lot of methods to write.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;~ What if I can tell you that you can transform the 10+ lines declaration, into a 3 lines one ?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;~ But, how ?!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With React Hooks feature, and this is how I'm going to convince you to give them a try.&lt;/p&gt;
&lt;h2&gt;
  
  
  Regular Class style exemple
&lt;/h2&gt;

&lt;p&gt;Let's put everything we talked about inside this component ...&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&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="c1"&gt;// State ..&lt;/span&gt;
      &lt;span class="na"&gt;user&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;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fetch API endpoint&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;componentDidUpdate&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;prevState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Apply effect based on changes ..&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;componentWillUnmount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Cancel subscriptions ..&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt;// Your app tree&amp;lt;/div&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;mapStateToProps&lt;/span&gt; &lt;span class="o"&gt;=&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="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="c1"&gt;// Return some element from your state ..&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;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dispatch&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="c1"&gt;//Return some functions that dispatch for you in the store ..&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="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, &lt;strong&gt;without&lt;/strong&gt; any logic specific code and JSX, the minimum required amount of code start to be substantial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functionnal Component exemple
&lt;/h2&gt;

&lt;p&gt;Now the fun part, let's rewrite everything using some Hooks. Don't worry, we will do it step-by-step.&lt;/p&gt;

&lt;p&gt;First, let's initialze your state:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt;// Your app ..&amp;lt;/div&amp;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="nx"&gt;App&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 recap what is going on here. We use a React Hook called &lt;strong&gt;useState()&lt;/strong&gt; which return a value, and a method to update this value. We also use the nice destructration synthax to get a super nice one liner.&lt;br&gt;
(More informations on the useState &lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Next, let's add our lifecycles:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&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;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&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="nx"&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;// Equivalent of componentDidUpdate&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="c1"&gt;// Equivalent of componentWillUnmount&lt;/span&gt;

    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt; &lt;span class="c1"&gt;// Equivalent of componentDidUpdate.&lt;/span&gt;
    &lt;span class="c1"&gt;// It's an array of dependencies, add values there,&lt;/span&gt;
    &lt;span class="c1"&gt;// and React will re execute the useEffect whenever one of value changes&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="c1"&gt;// Your app ..&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing, isnt it ? You can find more more infos &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;here&lt;/a&gt;. But basically, React will always execute what is inside he useEffect on the first render, and will watch the value passed into the array to re-run the effect again. You'll return what you want to execute when the component unmount.&lt;/p&gt;

&lt;p&gt;And last but not least, let's use Redux in there :&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&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;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;useSelector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Give access to the dispatch function&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSelector&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;=&amp;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;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Recover the value from the store&lt;/span&gt;

    &lt;span class="nx"&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;// Equivalent of componentDidUpdate&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="c1"&gt;// Equivalent of componentWillUnmount&lt;/span&gt;

    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt; &lt;span class="c1"&gt;// Equivalent of componentDidUpdate. It's an array of dependencies, add values there, and React will re execture the useEffect whenever one of value changes&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="c1"&gt;// Your app ..&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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="nx"&gt;App&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 is pretty straightforward, buy it's only some very basic usages, you can gather more infos &lt;a href="https://react-redux.js.org/api/hooks"&gt;here&lt;/a&gt;. But bascially, you now have access to the dispatch function, and to whatever value you want without having to mapDispatch and mapState. Without even the connect, and our component is now connected &lt;em&gt;implicitely&lt;/em&gt;.&lt;/p&gt;

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

&lt;p&gt;Well, nothing to conclude on, really. But take a second, and compare both code snippets. The result is stunning, you saved so much code for doing the exact same thing.&lt;br&gt;
I'll link you the react official documentation &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;here&lt;/a&gt;, and you can scroll down to a youtube video that does an amazing convertion from class to hooks.&lt;/p&gt;

&lt;p&gt;Now, tell me, are you convinced to give them a try ?&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

&lt;p&gt;Have a nice day.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How does React work under the hood (JSX wise) ?</title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Sun, 11 Oct 2020 20:40:58 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/how-does-react-work-under-the-hood-jsx-wise-44pd</link>
      <guid>https://dev.to/chandelieraxel/how-does-react-work-under-the-hood-jsx-wise-44pd</guid>
      <description>&lt;p&gt;You're using JSX every days without a clue how React does his &lt;em&gt;magic&lt;/em&gt; ? Ever wondered why we do have to wrap our elements in a parent ? (&lt;strong&gt;JSX expressions must have one parent element.&lt;/strong&gt; 🤯🤯🤯)&lt;/p&gt;

&lt;p&gt;Well, this article is for you. I'll do my very best to explain it to you, as I understood it. Keep in mind that's nobody is perfect, and if there's any mistake made, feel free to discuss it on Twitter, we're all learning everyday :D.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does our JSX work ?
&lt;/h2&gt;

&lt;p&gt;First things first, we have to make sure that you actually know how to insert new elements into your HTML with JavaScript. If you already know that, feel free to skip, if you don't, well ... Keep reading.&lt;/p&gt;

&lt;p&gt;In a regular HTML/JS website, here's how you would do :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="c1"&gt;// You first get the targetted element&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&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="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Then you create the new one&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newChildren&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="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// And you append it to the parent it belong to&lt;/span&gt;
  &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newChildren&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straightforward, right ? But you noticed that it does create an &lt;em&gt;empty&lt;/em&gt; element, you probably want to add at least some text, and even some attributes such as an id.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&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="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&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;newChildren&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="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;newChildren&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;newChildren&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World !&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newChildren&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your HTML page would now render a div, with an id of 'children', containing the 'Hello World' text, and so on for any other elements you want to create (you could write functions to help you out, but that's not the point or this article). It can become a mess really quickly, right ?&lt;br&gt;
You would have to handle every attributes you want to add, every listeners, etc. You get the idea.&lt;/p&gt;
&lt;h2&gt;
  
  
  Now, how does React work ?
&lt;/h2&gt;

&lt;p&gt;React does expose to use 2 libraries for the web developement : &lt;em&gt;React&lt;/em&gt; and &lt;em&gt;ReactDOM&lt;/em&gt;. Let's say you do have initialized your React project from create-react-app and it's running properly. Ultimately, once you have removed everything that is not necessery, you have a code looking 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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&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-dom&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="nx"&gt;App&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;./App&lt;/span&gt;&lt;span class="dl"&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="o"&gt;&amp;lt;&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;StrictMode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.StrictMode&amp;gt;&lt;/span&gt;&lt;span class="err"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&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;Let's get rid of the abstraction for now, and remove the JSX syntax, we'll come back on it later.&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&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="nx"&gt;App&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;./App&lt;/span&gt;&lt;span class="dl"&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="cm"&gt;/* Insert your elements here */&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;This function is the entry point of your React App. You are telling React to get a div with the id of 'root', and to render inside of it everything you'll pass as the first argument.&lt;/p&gt;

&lt;p&gt;Now, how do we actually create elements ? This render function won't accept anything that is not a valid React element. Let's get into it with the raw React API we imported.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;element&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&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;The create element function take 3 arguments :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The type of the HTML element you want to create (div, span, input...)&lt;/li&gt;
&lt;li&gt;Some &lt;em&gt;props&lt;/em&gt; that I'll explain juste after. For now the value is null as we don't want any&lt;/li&gt;
&lt;li&gt;And the &lt;em&gt;children&lt;/em&gt; which is basically anything that will be inserted inside this new element created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, what if we want to give an id to this element ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&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="s2"&gt;Hello World&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;This is where the second argument is used. It does accept an object of properties that will be applies to your element, here we added an id, but you could do it for a class, or some specific attributes for your tag. Even on onclick event !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;onClick&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="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="s2"&gt;Hello&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&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;Way better than the regular JS declaration.&lt;/p&gt;

&lt;p&gt;(As a side note, keep in mind that the last parameter is not mandatary, and you could give in the props with the children key)&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="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&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;What if we have more than one child to render inside your div ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;onClick&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="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="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&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="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;span&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="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;span&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="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;span&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="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo Bar !&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 children property accept an array of elements, and obviously you could do that as long as you want, and this is actually how your JSX code look like in reality.&lt;/p&gt;

&lt;p&gt;If you have been using React for a bit before reading this, you should now have a better insight of why you're doing certain things (such as &lt;code&gt;style={{color: 'red'}})&lt;/code&gt;, but we'll come on it later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Well, I ain't writing that anyway, how does this is useful ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Indeed, this is pretty annoying to write, and nobody using React will use it with the raw API. That's where React introduced JSX.&lt;/p&gt;

&lt;p&gt;JSX is basically a sugar synthax for writing the code above, thanks to Babel. (If you don't know what Babel is, it bascially take your code, and convert it into a browser compatibile version, more infos &lt;a href="https://babeljs.io/"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So if you write that :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;component&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt; &lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It'll actually be compiled by Babel as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;component&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&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="s2"&gt;Hello world&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;Now, what if we rewrite the previous example with the list of elements in JSX ? It would look 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;component&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&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="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="nx"&gt;Bar&lt;/span&gt; &lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing, isnt it ? It's way cleaner than the raw React API.&lt;/p&gt;

&lt;p&gt;Let's recap what we learned so far, but starting from JSX :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You write your JSX code, which is compiled by Babel to make actually readable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The result is a call to the &lt;strong&gt;React.createElement()&lt;/strong&gt; function with the correct parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And now what ? Well, React is doing one more trick for us : He's making one more abstraction, and doing the &lt;strong&gt;document.createElement()&lt;/strong&gt; function for us.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an exemple, I've been digging and I found a pseudo code wrote by &lt;a href="https://twitter.com/dan_abramov"&gt;Dan Abramov&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;node&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="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&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;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;propName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;propName&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&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;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;propName&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;propName&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;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;childElement&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;var&lt;/span&gt; &lt;span class="nx"&gt;childNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;childElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;childNode&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 see that React is doing exactly what we did at the beginning, create a new node, setting attributes if needed and appending it into the DOM with the help of the virtual DOM (I'll probably talk about it in another blog post).&lt;/p&gt;

&lt;p&gt;You can also find the full pseudo code &lt;a href="https://jsbin.com/qiguyibolu/1/edit?js,output"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Miscellaneous
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why I am passing an object for style inside the JSX ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whenever you'll want to apply inline style to your JSX element, you'll have to wrap the styles inside an object. Why ? Because doing the following won't make any sense :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;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;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;onClick&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="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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="c1"&gt;// Your browser would obviously complain&lt;/span&gt;
        &lt;span class="na"&gt;style&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;red&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 World&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;Right ? And this is exactly what you're telling Babel to do by writing 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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's also why you can't embed any kind of statements inside your JSX, such as if...else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How Babel does understand the difference between an html tag, and a custom component ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By capitalizing your component. That's all. If you create a component without capitalizing it, Babel will understand it as a potention html tag, and so will create it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;component&amp;gt;&lt;/span&gt;My custom component&lt;span class="nt"&gt;&amp;lt;/component&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not what we wan't.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why do we need to wrap our elements into one parent ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's because on how the React API work, let's say you write 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;component&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt; &lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React will complain about having a parent, because Babel will compile it 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;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;div&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;children&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 World !&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;ae&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;Weird again, right ? You could wrap everything into an array, and return 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;component&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;World&lt;/span&gt; &lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 this is not really how it is suppose to be used, hence why you need a parent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Ending&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll wrap it up for now, hope your enjoyend and learnt something. Again, feel free to send my feedback nor mistakes, I'll appreciate it !&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

&lt;p&gt;Have a nice day.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Why you should cancel your API calls in React ? </title>
      <dc:creator>Chandelier Axel</dc:creator>
      <pubDate>Mon, 05 Oct 2020 16:32:38 +0000</pubDate>
      <link>https://dev.to/chandelieraxel/why-you-should-cancel-your-api-calls-in-react-410l</link>
      <guid>https://dev.to/chandelieraxel/why-you-should-cancel-your-api-calls-in-react-410l</guid>
      <description>&lt;h1&gt;
  
  
  Explanation
&lt;/h1&gt;

&lt;p&gt;We all know it, every web application make API calls, even the tiniest do so. ( Remember that first Todo app you made with a cat API ?).&lt;/p&gt;

&lt;p&gt;You’ll gather some data, render it and do whatever you like on the website.. The real problems arises when either your internet is slow, or the data you need to gather is huge. &lt;/p&gt;

&lt;p&gt;Imagine if you need to gather that huge amount of data with barely an Edge connection.. It will take a couple of seconds, at least, to get everything from the API endpoint, right ? What if your user go on another page ?&lt;/p&gt;

&lt;p&gt;If you thinked :&lt;/p&gt;

&lt;p&gt;&lt;em&gt;« Uh - Nothing, it’s not a big deal anyway. At most the data is uploaded and nothing will happen. »&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well.. You are partially right, NOTHING will happen. Thanks to our job, making mistakes is riskless - most of the time at least, but there’s always place for improvement and it’s our duty to free up as much network space as we can, and make our apps less data consuming.&lt;/p&gt;

&lt;p&gt;Without forever teasing, let’s dive straight into it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cancelling the calls
&lt;/h1&gt;

&lt;p&gt;First, I created a simple app which render only 2 components : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One who will be responsible to make the API call, and to render a button who will handle the redirection.&lt;/li&gt;
&lt;li&gt;The other component will only render a string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;apiCallDone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setApiCallDone&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&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;apiCallDone&lt;/span&gt;
                &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AnotherComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ApiCallComponent&lt;/span&gt; &lt;span class="nx"&gt;redirectToOtherComponent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setApiCallDone&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;As you can see, once the apiCall will be set as true, App.js will re-render and show the other component.&lt;/p&gt;

&lt;p&gt;Now let’s take a look at the component that make the actual call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ApiCallComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;redirectToOtherComponent&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;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setResult&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="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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://pokeapi.co/api/v2/pokemon/12&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;redirect&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;redirectToOtherComponent&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;redirect&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;Let&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;s call the APi &amp;lt;/button&amp;gt;
)
};

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

&lt;/div&gt;



&lt;p&gt;As you can see I reproduce a really simple component, that will make a call to the Pokémon API as soon as it’ll mount. And the button will trigger the function we passed in the props.&lt;/p&gt;

&lt;p&gt;Nothing fancy, right ? We literally made a really minimal representation of all our apps - Gather data, consume it, and possibly show a different view / redirect.&lt;/p&gt;

&lt;p&gt;Now let’s add some lags into our call by adding a timeout. Doing so we’ll mimic the slow internet.&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;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="nf"&gt;setTimeout&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://pokeapi.co/api/v2/pokemon/12&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;// Handle error ..&lt;/span&gt;
                &lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&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;Now let’s try to make our call, and to click on the button within the 3 seconds timer ..&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr1alxnanylujgd299mmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr1alxnanylujgd299mmf.png" alt="error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s what we were looking for. And I bet you know what this error is. It means that you’re trying to update a component state while the component was unmounted. In our exemple, it’s literally because we didn’t cancel our api call on the unmount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetch cancel
&lt;/h2&gt;

&lt;p&gt;To fix this with the fetch API :&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;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;// First, create a controller, more infos there : https://developer.mozilla.org/en-US/docs/Web/API/AbortController&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AbortController&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;setTimeout&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;// Then give it in the fetch options, so the controller is properly linked&lt;/span&gt;
        &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://pokeapi.co/api/v2/pokemon/12&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="na"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;// Handle error ..&lt;/span&gt;
            &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Then on the "unmount" of the component, abort the API call ..&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="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&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's all !&lt;/p&gt;

&lt;h2&gt;
  
  
  Axios
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;// More informations about the cancelation for the axios library here : https://github.com/axios/axios#cancellation&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CancelToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CancelToken&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;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;CancelToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;source&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nf"&gt;setTimeout&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;axios&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://pokeapi.co/api/v2/pokemon/12&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="na"&gt;cancelToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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="nf"&gt;setResult&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;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Handle error..&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="mi"&gt;3000&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="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&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;Congratulation ! You’ve now cleared your console from this filthy errors ! &lt;/p&gt;

&lt;p&gt;No more excuses when you'll create an API call, you now have all the tools to handle it properly.&lt;/p&gt;

&lt;p&gt;You can find the original article on the &lt;a href="https://othrys.dev/author/axel/" rel="noopener noreferrer"&gt;Othrys website&lt;/a&gt; and you can follow my &lt;a href="https://twitter.com/ChandelierAxel" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or tag me here to discuss about this article. &lt;/p&gt;

&lt;p&gt;Have a nice day !&lt;/p&gt;

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