<?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: Kenneth Lum</title>
    <description>The latest articles on DEV Community by Kenneth Lum (@kennethlum).</description>
    <link>https://dev.to/kennethlum</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%2F340159%2Fe265ab73-d2b5-454f-90ba-ccc8990cfcc0.png</url>
      <title>DEV Community: Kenneth Lum</title>
      <link>https://dev.to/kennethlum</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kennethlum"/>
    <language>en</language>
    <item>
      <title>The paradigm of React</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Mon, 18 Sep 2023 09:02:49 +0000</pubDate>
      <link>https://dev.to/kennethlum/the-paradigm-of-react-4b3m</link>
      <guid>https://dev.to/kennethlum/the-paradigm-of-react-4b3m</guid>
      <description>&lt;p&gt;I think it may be important to know one of the top principles of React, because without it, the code may turn out a bit non-idiomatic React, and it may need refactoring to bring it back to the React Way.  This one major principle is: use declarative ways to write your components.&lt;/p&gt;

&lt;p&gt;Sometimes, it may need to be imperative, but those are supposed to change a state, so that the state can change into something you declared earlier.&lt;/p&gt;

&lt;p&gt;For example, you may say that, when you have an array, you show the list as:&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="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;arr&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;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&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;So you are declaring: with the &lt;code&gt;arr&lt;/code&gt;, what should the output be.&lt;/p&gt;

&lt;p&gt;Now again, if you have filtering of the items, if the user can enter a keyword, then the code becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keyword&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filteredArray&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;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&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;You may not need to use &lt;code&gt;useMemo&lt;/code&gt; for the &lt;code&gt;filteredArray&lt;/code&gt;. However, imagine if you have a case where, if the user types in keyword, and needs to click on the "Filter" button to do the filtering, then you don't want to do the costly filtering for every user keypress. So if your code causes the main component to re-render every time when there is a keypress, then you may want to use the &lt;code&gt;useMemo&lt;/code&gt; to do the filtering, depending on what &lt;code&gt;keywordConfirmed&lt;/code&gt; is, when the user clicks on the "Filter" button. (There is a way to let a sub-component not cause the main component to re-render on each user keypress, so the filtering won't be done every time, but it is beyond the scope of this post).&lt;/p&gt;

&lt;p&gt;There is also this idea that: if the slowest computer and smartphone can handle your task of filtering every time in a super fast way, then, you may not need to use &lt;code&gt;useMemo&lt;/code&gt;.  Optimize it only when it is necessary. Note that this is different from the LeetCode way, when &lt;code&gt;n&lt;/code&gt; can be 5 million or 5 billion. Here, &lt;code&gt;n&lt;/code&gt; could be 50 in your use case, or 500, so optimizing in such cases can be viewed as premature optimization. (Donald Knuth: "The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.")&lt;/p&gt;

&lt;p&gt;Now what we have so far are all declarative: when &lt;code&gt;arr&lt;/code&gt; is such, and &lt;code&gt;keyword&lt;/code&gt; is such, then the output will be such: meaning it is like a function: what goes in, what goes out, and it is functional, or declarative. If you are familiar with the programming language Lisp, that's how it works.&lt;/p&gt;

&lt;p&gt;One note about this: when you consider the states, in general, try to have as few of them as possible. Also, look at what needs to be the state, and what are values that can be derived from the state. For example, in the example above, &lt;code&gt;arr&lt;/code&gt; is the state, and &lt;code&gt;filteredArray&lt;/code&gt; can be derived from it. So don't make &lt;code&gt;filteredArray&lt;/code&gt; a state, but make it a local, derived value inside this functional component.&lt;/p&gt;

&lt;p&gt;We also use these states and write the rest of the component using them.  For example, we may have&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;{&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it is again, declarative.&lt;/p&gt;

&lt;p&gt;Now there are times we need to do imperative operations, such as to fetch the value when the component is first rendered. (it could be this component or a higher up component).&lt;/p&gt;

&lt;p&gt;To do that, it'd be&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="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="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="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;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="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&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;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setArr&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;console&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="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here, we fetch data from the server, and set it to the &lt;code&gt;arr&lt;/code&gt; state. And from here, everything becomes declarative again.&lt;/p&gt;

&lt;p&gt;The same with event handlers. You'd usually write the handler to change one or more states, so that the declarative code will handle the rest.&lt;/p&gt;

&lt;p&gt;The rule is: usually it is either the user doing something, or the network sends back some data, or &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt; triggers some change, and therefore changes the state. From that point on, the declarative mechanism handles the rest.&lt;/p&gt;

&lt;p&gt;In other words, it is events: user interactions, timer events, network events, that change the states, and then from there, everything is handled in a declarative way. That is: you don't have to do anything.&lt;/p&gt;

&lt;p&gt;This concept is important, because if you use React, and you write it without thinking mostly declarative, and use both declarative and imperative ways, then your React code will not idiomatic and it may be against the overall paradigm that React is using.&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
&lt;a href="https://codesandbox.io/s/funny-hugle-94j2cj?file=/src/App.js"&gt;https://codesandbox.io/s/funny-hugle-94j2cj?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://react.dev/learn/managing-state"&gt;https://react.dev/learn/managing-state&lt;/a&gt;&lt;br&gt;
Note that there are seven sections for this topic.&lt;/p&gt;

</description>
      <category>react</category>
      <category>declarative</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Creating a new React app in a few seconds</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Thu, 07 Sep 2023 15:32:02 +0000</pubDate>
      <link>https://dev.to/kennethlum/creating-a-new-react-app-in-a-few-seconds-47b9</link>
      <guid>https://dev.to/kennethlum/creating-a-new-react-app-in-a-few-seconds-47b9</guid>
      <description>&lt;p&gt;There is a method to create a new React app quickly, much faster than &lt;code&gt;create-react-app&lt;/code&gt; or by Next.js&lt;/p&gt;

&lt;p&gt;The method is by Vite.  Assuming you are using a recent version of npm (7 or above), the following steps will create a new React app in seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create vite@latest myapp &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--template&lt;/span&gt; react
code &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;myapp
npm i
npm run dev &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--open&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't use Visual Studio Code or don't have the command line to start it already set up, then you can remove the line &lt;code&gt;code .&lt;/code&gt; or replace it with a command line that will start your editor if you want.&lt;/p&gt;

&lt;p&gt;You also can create an alias and put it in your &lt;code&gt;.bashrc&lt;/code&gt;:&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="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;vitecreate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'npm create vite@latest myapp -- --template react &amp;amp;&amp;amp; code . &amp;amp;&amp;amp; cd myapp &amp;amp;&amp;amp; npm i &amp;amp;&amp;amp; npm run dev -- --open'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then you can:&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="nb"&gt;mkdir &lt;/span&gt;MyNewReactProject
&lt;span class="nb"&gt;cd &lt;/span&gt;MyNewReactProject
vitecreate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and start doing development work right away. You can use codesandbox.com too, but if you want a local server running, together with your favorite IDE and being able to do git commits, you can use this Vite method.&lt;/p&gt;

&lt;p&gt;If you want to play with the SWC (Speedy Web Compiler) template, you can use &lt;code&gt;react-swc&lt;/code&gt; instead of &lt;code&gt;react&lt;/code&gt; after the &lt;code&gt;--template&lt;/code&gt; flag. For more information about different templates, see &lt;a href="https://vitejs.dev/guide"&gt;https://vitejs.dev/guide&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>vite</category>
      <category>createreactapp</category>
    </item>
    <item>
      <title>async / await concepts</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Tue, 23 May 2023 13:36:12 +0000</pubDate>
      <link>https://dev.to/kennethlum/async-await-concepts-26in</link>
      <guid>https://dev.to/kennethlum/async-await-concepts-26in</guid>
      <description>&lt;p&gt;To a traditional programmer, async / await can be tricky.  Here are some key concepts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There cannot be any &lt;code&gt;await&lt;/code&gt; inside of a function if this function is not declared as an async function.&lt;/li&gt;
&lt;li&gt;You can think of it this way: this async function returns to you immediately, and what does it return? It returns a promise that will be resolved or rejected later.&lt;/li&gt;
&lt;li&gt;Inside of the async function, the code will run, until it hits an &lt;code&gt;await&lt;/code&gt;.  Don't think of this &lt;code&gt;await&lt;/code&gt; as "waiting", but more like a &lt;code&gt;notifyMeOfTheValueWhenReady&lt;/code&gt; or &lt;code&gt;aValueThatWillBeReady&lt;/code&gt;. You can even read it as &lt;code&gt;stopHereAndGiveMeTheValueWhenReady&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To be more precise, the promise isn't returned to the caller until the first &lt;code&gt;await&lt;/code&gt; is reached. Typically you will have at least one &lt;code&gt;await&lt;/code&gt; inside an async function, or else you wouldn't use an async function in the first place. You can see in the examples below to verify the case: inside the async function, the code runs until the first &lt;code&gt;await&lt;/code&gt;, then the promise is returned to the caller. This usually does not matter, but it may be good to know.&lt;/li&gt;
&lt;li&gt;This &lt;code&gt;await&lt;/code&gt; is like a "set it and forget it". It is similar to setting an observer, if you are familiar with the observer pattern. Or you can think of it as setting a callback function. The &lt;code&gt;await&lt;/code&gt; is followed by a promise, like &lt;code&gt;await p;&lt;/code&gt; You can think of it as a &lt;code&gt;.then(v =&amp;gt; { ... })&lt;/code&gt;, so the value &lt;code&gt;v&lt;/code&gt; is what &lt;code&gt;await p&lt;/code&gt; will become in the future.&lt;/li&gt;
&lt;li&gt;When the execution of code reaches this &lt;code&gt;await&lt;/code&gt;, after the "set it and forget it", nothing is done. The code does not go onto the next line until this "future value", or promise is resolved or rejected.&lt;/li&gt;
&lt;li&gt;We can use &lt;code&gt;price = await getStockPrice();&lt;/code&gt; or &lt;code&gt;displayStockPrice(await getStockPrice());&lt;/code&gt;.  In the first case, that &lt;code&gt;await&lt;/code&gt; reads like &lt;code&gt;notifyMeOfTheValueWhenReady&lt;/code&gt;. In the second case, the &lt;code&gt;await&lt;/code&gt; reads like &lt;code&gt;aValueThatWillBeReady&lt;/code&gt;. For both cases, reading it as &lt;code&gt;stopHereAndGiveMeTheValueWhenReady&lt;/code&gt; goes well. In the second case, the &lt;code&gt;await&lt;/code&gt; cause the async function code to stop there, until a value is obtained, and then &lt;code&gt;displayStockPrice()&lt;/code&gt; is called.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;

&lt;p&gt;In the following, try and figure out the order of the lines are displayed.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;$(document).ready(function() { ... });&lt;/code&gt; is just jQuery's way of running the code when the DOM is ready in the jsfiddle environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/kwtomj59/"&gt;https://jsfiddle.net/kwtomj59/&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="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;Running the program&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am running at Point A of the async function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&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;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;finally I get the value from the async function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is after calling the async function&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;h2&gt;
  
  
  Example 2
&lt;/h2&gt;

&lt;p&gt;What about the following? Note that when the big loop is running, the UI of that input box is not reacting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/odr4sz58/"&gt;https://jsfiddle.net/odr4sz58/&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="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;Running the program&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;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;I am running at Point A of the async function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&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;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;finally I get the value from the async function, and it is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is after calling the async function&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;h2&gt;
  
  
  Example 3
&lt;/h2&gt;

&lt;p&gt;Since the &lt;code&gt;console.log&lt;/code&gt; is not executed until later, we can use &lt;code&gt;alert&lt;/code&gt; to see the immediate message:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/odr4sz58/1/"&gt;https://jsfiddle.net/odr4sz58/1/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is just the above program with the &lt;code&gt;console.log&lt;/code&gt; replaced with &lt;code&gt;alert&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 4
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/hpj4tvy3/"&gt;https://jsfiddle.net/hpj4tvy3/&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="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Running the program&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&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="o"&gt;=&amp;gt;&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am running at Point A of the async function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&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="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;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&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;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`finally I get the value from the async function, and it is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is after calling the async function&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;h2&gt;
  
  
  Example 5
&lt;/h2&gt;

&lt;p&gt;Finally, here is an example of the &lt;code&gt;fn(await ...)&lt;/code&gt; pattern. What is the order of the messages being displayed?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/yb0th3cw/"&gt;https://jsfiddle.net/yb0th3cw/&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="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Running the program&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&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="o"&gt;=&amp;gt;&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))));&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am running at Point A of the async function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&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;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&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;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`finally I get the value from the async function, and it is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is after calling the async function&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;



</description>
      <category>javascript</category>
      <category>async</category>
      <category>await</category>
      <category>asynchronous</category>
    </item>
    <item>
      <title>Editing and using a command quite some time ago on Bash</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Thu, 23 Feb 2023 11:41:21 +0000</pubDate>
      <link>https://dev.to/kennethlum/editing-and-using-a-command-quite-some-time-ago-pj4</link>
      <guid>https://dev.to/kennethlum/editing-and-using-a-command-quite-some-time-ago-pj4</guid>
      <description>&lt;p&gt;In Bash or Zsh, we can in general use the up arrow to get back the command line some 20 or 25 lines ago, edit the command, press Enter and execute it.&lt;/p&gt;

&lt;p&gt;However, pressing the up arrow key 20 or 25 times can be tedious, and we need to look closely when we get that exact line, which can be tiring on the eyes.&lt;/p&gt;

&lt;p&gt;One quicker way is this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;h&lt;/code&gt; or &lt;code&gt;history&lt;/code&gt; to look at the list of past commands. When needed, use &lt;code&gt;history -300&lt;/code&gt; to look at 300 lines of history&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;!127&lt;/code&gt; to execute the command line 127 as shown above "as is" with no editing, but to edit it, then:&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;!127:p&lt;/code&gt; to just print that command line without executing it, and put it into history&lt;/li&gt;
&lt;li&gt;Then, use the up arrow once and you can get that command, and edit it as you wish (using left, right arrow, CTRL-A to go to the beginning, or CTRL-R to search backward, etc, depending on your settings)&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>bash</category>
      <category>zsh</category>
      <category>history</category>
    </item>
    <item>
      <title>Understanding what a Blob is</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Sat, 08 May 2021 22:44:03 +0000</pubDate>
      <link>https://dev.to/kennethlum/understanding-what-a-blob-is-35ga</link>
      <guid>https://dev.to/kennethlum/understanding-what-a-blob-is-35ga</guid>
      <description>&lt;p&gt;There is a little bit of mystery of what a Blob is.&lt;/p&gt;

&lt;p&gt;A Blob is a "B L OB" or "Binary Large Object".  It is as if it is a file.&lt;/p&gt;

&lt;p&gt;It is &lt;a href="https://w3c.github.io/FileAPI/#dfn-Blob" rel="noopener noreferrer"&gt;defined&lt;/a&gt; in the &lt;a href="https://w3c.github.io/FileAPI" rel="noopener noreferrer"&gt;File API&lt;/a&gt; of the JS specs.  It has&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a series of immutable bytes&lt;/li&gt;
&lt;li&gt;a size&lt;/li&gt;
&lt;li&gt;an MIME type, such as &lt;code&gt;'image/jpeg'&lt;/code&gt;, &lt;code&gt;'text/plain'&lt;/code&gt;, &lt;code&gt;'text/csv'&lt;/code&gt;, &lt;code&gt;'application/pdf'&lt;/code&gt;,  or &lt;code&gt;'application/json'&lt;/code&gt; (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types" rel="noopener noreferrer"&gt;some more examples&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if from the database or from &lt;a href="https://developers.google.com/protocol-buffers" rel="noopener noreferrer"&gt;protobuf&lt;/a&gt;, we can get an array of bytes, which is the byte dump of an image file, we can compose it back to a "file" which is like &lt;code&gt;my-awesome-file.jpg&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In JavaScript Definitive Guide, 7th ed, p. 522, it is said that the file may actually be saved on user's local hard drive, and the JavaScript can still access the Blob.&lt;/p&gt;

&lt;p&gt;We can look at an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/KennethLum/9L51my3b/" rel="noopener noreferrer"&gt;https://jsfiddle.net/KennethLum/9L51my3b/&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;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrUint8&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;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;blob&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;Blob&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;arrUint8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image/jpeg&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&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="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#my-image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;arr&lt;/code&gt; is a regular JavaScript array, and its content is not listed above but is in the JSFiddle example.&lt;/p&gt;

&lt;p&gt;It is first converted to a "typed array", which is an array similar to the low level memory block when a C program is written. (think &lt;code&gt;malloc&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;It is then converted to a "blob", and it is as if it is a file.  Then how can we access this file?  One way is to get a URL for it, and that's what the line&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;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is for.  The URL may appear like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blob:https://fiddle.jshell.net/6631b3ce-ba72-41f1-bfb0-e498862a573d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it is like, this URL is the address of a file.&lt;/p&gt;

&lt;p&gt;Then in the above example, we just set the image element's &lt;code&gt;src&lt;/code&gt; to point to that URL, and we can see the image.&lt;/p&gt;

&lt;h2&gt;
  
  
  Letting the user automatically download the file
&lt;/h2&gt;

&lt;p&gt;There is one more trick to automatically let the user download this file, which is the use the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag. We set the &lt;code&gt;href&lt;/code&gt; and the &lt;code&gt;download&lt;/code&gt; attribute of the tag, and then use JavaScript to click on it:&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;imgElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#my-image&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;anchorElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#the-link&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;imgElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my-awesome-image.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;imgElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;load&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&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;Example: &lt;a href="https://jsfiddle.net/KennethLum/t89xguf6/" rel="noopener noreferrer"&gt;https://jsfiddle.net/KennethLum/t89xguf6/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the file can be automatically downloaded for the user.  This can be useful, when some image is made (as a Blob or from Canvas), and then it is provided to the user as a download. For example, we could make a shipping label, or a discount coupon with the user's name on it, or it can be a &lt;code&gt;.csv&lt;/code&gt; file with a stock's historical data in it, and then provide to the user as an image or as a PDF or &lt;code&gt;.csv&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;In fact, the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; element does not even need to be present on the page. It can be dynamically created:&lt;/p&gt;

&lt;p&gt;Example: &lt;a href="https://jsfiddle.net/KennethLum/gx6zu9ap/" rel="noopener noreferrer"&gt;https://jsfiddle.net/KennethLum/gx6zu9ap/&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="nx"&gt;imgElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;load&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="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;anchorElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-awesome-image.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;anchorElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&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;



</description>
      <category>javascript</category>
      <category>blob</category>
    </item>
    <item>
      <title>Automatically re-render the page when we save the file in the code editor</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Mon, 05 Apr 2021 14:08:03 +0000</pubDate>
      <link>https://dev.to/kennethlum/automatically-re-render-the-page-when-we-save-the-file-in-the-code-editor-56jp</link>
      <guid>https://dev.to/kennethlum/automatically-re-render-the-page-when-we-save-the-file-in-the-code-editor-56jp</guid>
      <description>&lt;p&gt;TLDR: use &lt;code&gt;live-server&lt;/code&gt; and we don't have to keep refreshing the webpage but the page would refresh itself when we change our code in the webpage.&lt;/p&gt;

&lt;p&gt;I used to use Ruby or Python to spin up a local web server, for example, when rendering a webpage as local HTML file cannot support data fetch, but requires a web server.&lt;/p&gt;

&lt;p&gt;And then, we always have to save the file, and go to the web browser, and refresh the page.&lt;/p&gt;

&lt;p&gt;But there is a server that can automatically re-renders the webpage. The advantage is that we can stay in the code editor, and save the file, and the browser next to the code editor will re-render the page. If we have a big monitor, we can move the browser at the top left corner of the screen, and the editor at the center of the screen, and see the full page of re-rendered result on the browser.&lt;/p&gt;

&lt;p&gt;The server is called &lt;code&gt;live-server&lt;/code&gt;. We first have to install &lt;code&gt;node&lt;/code&gt;, and then we can do&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;-g&lt;/span&gt; live-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then we can just &lt;code&gt;cd&lt;/code&gt; to our folder, and invoke&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;live-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I usually like to not have any &lt;code&gt;index.html&lt;/code&gt; file, so that &lt;a href="http://127.0.0.1:8080"&gt;http://127.0.0.1:8080&lt;/a&gt; gives me a list of files I can choose in the current directory.&lt;/p&gt;

&lt;p&gt;The way this is done is that &lt;code&gt;live-server&lt;/code&gt; add some JavaScript at the end of our file, so that when we save any file or update any file in the folder, then the page would refresh itself.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>To tell what the web server is, right inside of a webpage</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Fri, 02 Apr 2021 09:10:32 +0000</pubDate>
      <link>https://dev.to/kennethlum/to-tell-what-the-server-is-right-inside-of-a-webpage-2gl8</link>
      <guid>https://dev.to/kennethlum/to-tell-what-the-server-is-right-inside-of-a-webpage-2gl8</guid>
      <description>&lt;p&gt;Sometimes if we start a local webserver, or if we use other online pages, we would like to find out what webserver it is, and it is possible to do it right inside of the webpage.  We can fetch something, and look at the response header by &lt;code&gt;res.headers.get("Server")&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&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.txt&lt;/span&gt;&lt;span class="dl"&gt;"&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;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#server-note&lt;/span&gt;&lt;span class="dl"&gt;"&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="s2"&gt;`The server is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&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;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Demo: &lt;a href="https://jsfiddle.net/KennethKinLum/80td3s4f/"&gt;https://jsfiddle.net/KennethKinLum/80td3s4f/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The server for &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Webserver&lt;/th&gt;
&lt;th&gt;in the header&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Node.js's &lt;code&gt;live-server&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;nothing &lt;code&gt;(null)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ruby's &lt;code&gt;ruby -run -e httpd . -p 8080&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WEBrick/1.4.2 (Ruby/2.6.3/2019-04-16)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python 2's &lt;code&gt;python -m SimpleHTTPServer 8080&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SimpleHTTP/0.6 Python/2.7.16&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python 3's &lt;code&gt;python3 -m http.server 8080&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SimpleHTTP/0.6 Python/3.8.2&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JSFiddle&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nginx&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Codesandbox&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cloudflare&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;macOS Big Sur's webserver&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Apache/2.4.46 (Unix)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It is possible to tell the user-agent as well, using &lt;code&gt;navigator.userAgent&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>http</category>
      <category>webserver</category>
    </item>
    <item>
      <title>Building a simple slideshow component in React using Hooks</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Wed, 31 Mar 2021 07:53:45 +0000</pubDate>
      <link>https://dev.to/kennethlum/building-a-simple-slideshow-component-in-react-using-hooks-2ojd</link>
      <guid>https://dev.to/kennethlum/building-a-simple-slideshow-component-in-react-using-hooks-2ojd</guid>
      <description>&lt;p&gt;I was trying to build a simple slideshow component using React Hooks.  Initially, I was thinking of how to use calculations to position a picture properly on the page or inside of the component region.&lt;/p&gt;

&lt;p&gt;It turns out it is a lot simpler, if we use containers or containers that cover the whole viewport to contain a picture, and move the container accordingly.&lt;/p&gt;

&lt;p&gt;The reason is, if we calculate where to place the image, and what size to use, it can be a lot of calculations. If we use a container and set the &lt;code&gt;max-width&lt;/code&gt; and &lt;code&gt;max-height&lt;/code&gt;, and we center the picture in the container, such as by using the container as a flex box, then we don't have to do all the calculations and it is automatically handled by CSS.&lt;/p&gt;

&lt;p&gt;In the example here, I am trying to use the viewport, and to better use it using CSS, note that we can use the unit of &lt;code&gt;vw&lt;/code&gt; and &lt;code&gt;vh&lt;/code&gt;, which is viewport width and viewport height.&lt;/p&gt;

&lt;p&gt;I "dock" the containers at the left of the screen (hidden):&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;theLeftShift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&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;currentImageIndex&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;currentImageIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100vw&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;-100vw&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;and&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="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;nImages&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&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;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;theLeftShift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`./pic&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.jpg`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&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;We don't really need to move the containers further and further left, and can just dock it to the immediate left.  That way, we don't need to create a region that is wider than it is needed.&lt;/p&gt;

&lt;p&gt;The demo: &lt;a href="https://codesandbox.io/s/simple-slideshow-z2089?file=/src/App.js"&gt;https://codesandbox.io/s/simple-slideshow-z2089?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The full page demo: &lt;a href="https://z2089.csb.app/"&gt;https://z2089.csb.app/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>slideshow</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Using React in JSFiddle, Coderpad, or just an HTML page</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Fri, 26 Mar 2021 10:13:02 +0000</pubDate>
      <link>https://dev.to/kennethlum/using-react-in-jsfiddle-or-coderpad-na8</link>
      <guid>https://dev.to/kennethlum/using-react-in-jsfiddle-or-coderpad-na8</guid>
      <description>&lt;p&gt;Sometimes we may need to use React in Coderpad when we interview candidates or get interviewed.&lt;/p&gt;

&lt;p&gt;There is actually a way to do that easily in Coderpad:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Just change the language to &lt;code&gt;HTML&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Choose the Packages next to that, and choose &lt;code&gt;React&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then we can write code such 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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;
  &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://unpkg.com/react@17/umd/react.development.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;crossorigin&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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;script&lt;/span&gt;
  &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://unpkg.com/react-dom@17/umd/react-dom.development.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;crossorigin&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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;script&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://unpkg.com/babel-standalone@6/babel.min.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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="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;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;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;script&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/babel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setCount&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="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;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="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;handleClick&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;Click&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;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="nf"&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;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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;Demo in Coderpad (if it can allow public access): &lt;a href="https://app.coderpad.io/G7E9DQQT" rel="noopener noreferrer"&gt;https://app.coderpad.io/G7E9DQQT&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Demo in JSFiddle: &lt;a href="https://jsfiddle.net/d9m68rft/" rel="noopener noreferrer"&gt;https://jsfiddle.net/d9m68rft/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or demo in Codesandbox: &lt;a href="https://codesandbox.io/s/brave-leaf-6dmbu?file=/index.html" rel="noopener noreferrer"&gt;https://codesandbox.io/s/brave-leaf-6dmbu?file=/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One issue is that as of right now, Coderpad or JSFiddle cannot auto format our code with the JSX, and sometimes the code is a bit messy due to indentation.  Codesandbox can reformat everything on File -&amp;gt; Save, but some companies disallow moving the code to else where and pasting in back to Coderpad, to discourage cheating, probably.&lt;/p&gt;

&lt;p&gt;Coderpad current uses React 16.13.1 and it is good enough for React Hooks.  To be able to use React Hooks, we must use React 16.8 or above. &lt;br&gt;
 The scripts in the above code is suggested by the React documentation itself: &lt;a href="https://reactjs.org/docs/add-react-to-a-website.html" rel="noopener noreferrer"&gt;https://reactjs.org/docs/add-react-to-a-website.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also see the different versions of React files that can be included, in: &lt;a href="https://cdnjs.com/libraries/react" rel="noopener noreferrer"&gt;https://cdnjs.com/libraries/react&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JSFiddle has a React choice, but it requires a little bit of configuartion. The best website to use React is &lt;a href="https://codesandbox.io" rel="noopener noreferrer"&gt;https://codesandbox.io&lt;/a&gt; I found.&lt;/p&gt;

&lt;p&gt;Codersandbox is a bit tricky to share with another person. One time I had to constantly save the file, and ask the other person to constantly refresh her page.  I often had to ask, "do you see the updates?"&lt;/p&gt;

&lt;p&gt;It can in fact be quite easy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On the left side of the window, click on the bottom icon, which is "Share"&lt;/li&gt;
&lt;/ol&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%2Fb9lxt8uxj66cc6qf2wff.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%2Fb9lxt8uxj66cc6qf2wff.png" alt="Codesandbox to share"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;And then, copy that "live" link and share with the other person:&lt;/li&gt;
&lt;/ol&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%2F0yd9necouw2d0l7jeba8.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%2F0yd9necouw2d0l7jeba8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is somewhat a pity that some companies do not allow using Codesandbox because they have more control using Coderpad with replaying the interview.  But I suppose one way is to be so good in it, that the interviewer is totally impressed and can vow that you said and typed everything yourself, and vow to say that you are one of the best candidates he has met so far and recommend a strong hire.&lt;/p&gt;

</description>
      <category>react</category>
      <category>coderpad</category>
      <category>interview</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Caching a function instead of something "expensive" using useCallback() in React</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Sat, 20 Mar 2021 03:01:23 +0000</pubDate>
      <link>https://dev.to/kennethlum/caching-a-function-instead-of-something-expensive-c34</link>
      <guid>https://dev.to/kennethlum/caching-a-function-instead-of-something-expensive-c34</guid>
      <description>&lt;p&gt;We have seen that we can cache something that is "expensive", using &lt;code&gt;useMemo()&lt;/code&gt;, in &lt;a href="https://dev.to/kennethlum/seeing-usememo-speed-up-our-webpage-3h91"&gt;https://dev.to/kennethlum/seeing-usememo-speed-up-our-webpage-3h91&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now a function can be quite simple, but why would we want to cache it to?  It can be when we pass into a child component or use it else where, and we want to keep it the same value, so that there is no unnecessary re-rendering.&lt;/p&gt;

&lt;p&gt;We can see, in&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;App&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;myFooter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&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;Footer&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;, &lt;/span&gt;&lt;span class="se"&gt;[])&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;handleClick&lt;/code&gt; is a new function every time &lt;code&gt;App()&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;useMemo()&lt;/code&gt; to cache it too, just like how we cache &lt;code&gt;&amp;lt;Footer /&amp;gt;&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The code:&lt;/p&gt;

&lt;p&gt;Wrong behavior demo: &lt;a href="https://codesandbox.io/s/relaxed-newton-5sqmy?file=/src/App.js"&gt;https://codesandbox.io/s/relaxed-newton-5sqmy?file=/src/App.js&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;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="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;It can only increment the count to 1, but not more. Why is that?  The reason is that we cached the function, which is a closure with the scope chain with &lt;code&gt;count&lt;/code&gt; equal to &lt;code&gt;0&lt;/code&gt;. Every time, the function sees &lt;code&gt;count&lt;/code&gt; as &lt;code&gt;0&lt;/code&gt;, and therefore the &lt;code&gt;setCount(count + 1)&lt;/code&gt; is always &lt;code&gt;setCount(0 + 1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To fix that behavior, we can use:&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="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;Demo: &lt;a href="https://codesandbox.io/s/nameless-fast-d0fv1?file=/src/App.js"&gt;https://codesandbox.io/s/nameless-fast-d0fv1?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we don't need to use &lt;code&gt;useMemo()&lt;/code&gt;, but can use &lt;code&gt;useCallback()&lt;/code&gt;. It is essentially the same thing:&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;c&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;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Demo: &lt;a href="https://codesandbox.io/s/busy-archimedes-vse8f?file=/src/App.js"&gt;https://codesandbox.io/s/busy-archimedes-vse8f?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we don't need to give a function that return a value, but can provide that function we want to cache directly.&lt;/p&gt;

&lt;p&gt;Likewise, if we have&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is not going to work: &lt;a href="https://codesandbox.io/s/distracted-cloud-o93gw?file=/src/App.js"&gt;https://codesandbox.io/s/distracted-cloud-o93gw?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see that &lt;code&gt;handleClick&lt;/code&gt; is the same value (a reference to the same function), we can use a &lt;code&gt;useRef()&lt;/code&gt; to double check it.  We can skip this part if &lt;code&gt;useRef()&lt;/code&gt; is not familiar to you yet:&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;checkingIt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;c&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;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkingIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;checkingIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Demo: &lt;a href="https://codesandbox.io/s/unruffled-sunset-81vwx?file=/src/App.js"&gt;https://codesandbox.io/s/unruffled-sunset-81vwx?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the first time, the &lt;code&gt;console.log()&lt;/code&gt; would print out &lt;code&gt;false&lt;/code&gt;, but once we set it, the next time &lt;code&gt;App()&lt;/code&gt; is called, it has the same value as the previous time, and would print out &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we change it to a new function every time, then it would print out &lt;code&gt;false&lt;/code&gt; every time.&lt;/p&gt;

&lt;p&gt;Demo: &lt;a href="https://codesandbox.io/s/affectionate-dewdney-556mn?file=/src/App.js"&gt;https://codesandbox.io/s/affectionate-dewdney-556mn?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>reacthooks</category>
    </item>
    <item>
      <title>Seeing useMemo() speeds up our webpage</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Fri, 19 Mar 2021 05:48:01 +0000</pubDate>
      <link>https://dev.to/kennethlum/seeing-usememo-speed-up-our-webpage-3h91</link>
      <guid>https://dev.to/kennethlum/seeing-usememo-speed-up-our-webpage-3h91</guid>
      <description>&lt;p&gt;In some situations, we may not need to re-render a component every time.&lt;/p&gt;

&lt;p&gt;For example, if a component depends on a number &lt;code&gt;n&lt;/code&gt;, and our &lt;code&gt;n&lt;/code&gt; doesn't change. Or, if the page footer is quite complicated, and we make it so that our page does not re-render it every time because it is the same every time, we can use &lt;code&gt;useMemo()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As an example, we are simulating a page footer here, and to add to its "work", we calculate &lt;code&gt;69!&lt;/code&gt; (69 factorial). The value was the maximum number that can be displayed on a simple calculator.&lt;/p&gt;

&lt;p&gt;Demo: &lt;a href="https://codesandbox.io/s/vigilant-newton-5ocvf?file=/src/App.js"&gt;https://codesandbox.io/s/vigilant-newton-5ocvf?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can try clicking on the button to add the count on the page. The UI is quite responsive.&lt;/p&gt;

&lt;p&gt;To experiment, let's increase the factorial to &lt;code&gt;30000!&lt;/code&gt; (30,000 factorial), and here is a demo:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/priceless-platform-ifigu?file=/src/App.js"&gt;https://codesandbox.io/s/priceless-platform-ifigu?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that every time we click on the button, the update on the page is really slow.  That's because the &lt;code&gt;Footer&lt;/code&gt; component (or any costly component) is being re-rendered every time our &lt;code&gt;count&lt;/code&gt; is updated.&lt;/p&gt;

&lt;p&gt;(sidenote: it also feels much longer than the 0.36 seconds (as tested on a MacBook Air). This partly may be due to the &lt;code&gt;Footer&lt;/code&gt; component has a really wide width to accommodate the number, so the browser has to spend a lot of resource to render it.  If we merely prints out the binary length of the number, it is quite faster: &lt;a href="https://codesandbox.io/s/divine-https-i6chz?file=/src/App.js"&gt;https://codesandbox.io/s/divine-https-i6chz?file=/src/App.js&lt;/a&gt; But to keep it as slow as possible, we will print out the whole number again in the example below.)&lt;/p&gt;

&lt;p&gt;So we can use &lt;code&gt;useMemo()&lt;/code&gt; to memoize the component (memoizing what &lt;code&gt;Footer&lt;/code&gt; returns).&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;myFooter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&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;Footer&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;, &lt;/span&gt;&lt;span class="se"&gt;[])&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Demo: &lt;a href="https://codesandbox.io/s/brave-noyce-zby1w?file=/src/App.js"&gt;https://codesandbox.io/s/brave-noyce-zby1w?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we are saying this component doesn't not depend on anything, so we provide an empty array at the end of &lt;code&gt;useMemo()&lt;/code&gt;. If for any reason we depend on some value to render this component, we'd add it to this array. This is similar to how &lt;code&gt;useEffect()&lt;/code&gt; works for its dependency array.&lt;/p&gt;

&lt;p&gt;So here, when we click on the button, the page updates close to instantly, without going through the steps of getting the result from the &lt;code&gt;Footer&lt;/code&gt; rendering.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://reactjs.org/docs/hooks-reference.html#usememo"&gt;https://reactjs.org/docs/hooks-reference.html#usememo&lt;/a&gt; (Official docs)&lt;/p&gt;

&lt;p&gt;P.S. It is Sept 2023 right now, and I am using a MacBook Air M2. The original "slow" app is taking only 0.09 seconds to re-render, so the unresponsiveness cannot be felt as easily.  To change that, I have changed it to &lt;code&gt;60000!&lt;/code&gt; (60,000 factorial), and also display only the length of it, so that we are not testing the webpage needing a super wide area to display the number.  The example is at:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/priceless-platform-ifigu?file=/src/App.js"&gt;https://codesandbox.io/s/priceless-platform-ifigu?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and the version using &lt;code&gt;useMemo&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/cranky-kare-fgp48m?file=/src/App.js"&gt;https://codesandbox.io/s/cranky-kare-fgp48m?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
      <category>usememo</category>
    </item>
    <item>
      <title>The two general patterns of useEffect() in React Hooks</title>
      <dc:creator>Kenneth Lum</dc:creator>
      <pubDate>Wed, 17 Mar 2021 04:29:51 +0000</pubDate>
      <link>https://dev.to/kennethlum/the-two-general-patterns-of-useeffect-in-react-hooks-3lga</link>
      <guid>https://dev.to/kennethlum/the-two-general-patterns-of-useeffect-in-react-hooks-3lga</guid>
      <description>&lt;p&gt;When we use &lt;code&gt;useEffect()&lt;/code&gt;, there are two common patterns.  Let's start with the easier 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="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;// do the set up&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// do the take down&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are providing an empty dependency array at the end, so the provided function will only execute once.  It is for "set up" and "take down".  &lt;/p&gt;

&lt;p&gt;Where it says &lt;code&gt;// do the set up&lt;/code&gt;, it is to do the set up, such as fetching data, or getting some data from &lt;code&gt;localStorage&lt;/code&gt;, or set up any &lt;code&gt;setInterval()&lt;/code&gt; or &lt;code&gt;setTimeout()&lt;/code&gt;.  This is similar to &lt;code&gt;componentDidMount&lt;/code&gt; back in the days of class components.&lt;/p&gt;

&lt;p&gt;Then that function &lt;em&gt;returns&lt;/em&gt; another function, and this function is to "take down" or "clean up".  It is where &lt;code&gt;// do the take down&lt;/code&gt; is up above. This is similar to &lt;code&gt;componentWillUnmount&lt;/code&gt; in the class components.&lt;/p&gt;

&lt;p&gt;So it is quite clear: one set up, one clean up.&lt;/p&gt;

&lt;p&gt;The pattern above is to run once. The other pattern is to "run often".&lt;/p&gt;

&lt;p&gt;It can be&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;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;// do the set up&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// do the take down&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&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;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;// do the set up&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// do the take down&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;friendID&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the first form does not have the dependency array at all.  This way, it is to run every time. Typically, it is to "take down" once, and to "set up" once, having a "small loop" between our grand "set up" and "take down".  It is similar to what we do for &lt;code&gt;componentDidUpdate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code below that is to run every time when &lt;code&gt;friendID&lt;/code&gt; changes, and it is for example, to "take down" the subscription of whether the friend is online or not, and then subscribe to another friend's online status to see whether the friend is online or not. So we can view it as a tiny take down, and a tiny set up.&lt;/p&gt;

&lt;p&gt;So that's basically it, two actions for &lt;code&gt;componentDidMount&lt;/code&gt; and &lt;code&gt;componentWillUnmount&lt;/code&gt;, and then the "tiny actions" or a tiny loop, each time for a &lt;code&gt;componentDidUpdate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And if we have to do something for the "grand" set up and take down, and then a "tiny loop", and the actions are quite different for them, then we can use two &lt;code&gt;useEffect()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;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;// do the set up&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// do the take down&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;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;// do the set up&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="c1"&gt;// do the take down&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;friendID&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so the first &lt;code&gt;useEffect()&lt;/code&gt; is to run once, for the grand set up and take down, and the second one is for the "tiny loop". &lt;/p&gt;

&lt;p&gt;Reference: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;https://reactjs.org/docs/hooks-effect.html&lt;/a&gt; (Official docs)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/hooks-overview.html"&gt;https://reactjs.org/docs/hooks-overview.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wattenberger.com/blog/react-hooks"&gt;https://wattenberger.com/blog/react-hooks&lt;/a&gt; by Amelia Wattenberger&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/"&gt;https://overreacted.io/a-complete-guide-to-useeffect/&lt;/a&gt; by Dan Abramov&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
