<?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: Benjamin Liu</title>
    <description>The latest articles on DEV Community by Benjamin Liu (@parkrooben).</description>
    <link>https://dev.to/parkrooben</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%2F274143%2Fc0b0b81c-288d-4dd6-bc29-28de6f98aacc.png</url>
      <title>DEV Community: Benjamin Liu</title>
      <link>https://dev.to/parkrooben</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parkrooben"/>
    <language>en</language>
    <item>
      <title>Memoisation in React</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Sat, 13 Mar 2021 03:22:49 +0000</pubDate>
      <link>https://dev.to/parkrooben/memoisation-in-react-dmf</link>
      <guid>https://dev.to/parkrooben/memoisation-in-react-dmf</guid>
      <description>&lt;p&gt;Memoisation is an optimisation technique that caches the result of previous computations so that they can be quickly accessed without repeating the same computation.&lt;/p&gt;

&lt;p&gt;React introduces quite a few memoisation functions being &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. React.memo
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;React.memo&lt;/code&gt; is a higher order component when wrapped around a component, memoises the result of the component and does a &lt;strong&gt;shallow&lt;/strong&gt; comparison before the next render. If the new props are the same the component doesn't re-render and uses the memoised result.&lt;/p&gt;

&lt;p&gt;By default &lt;code&gt;memo&lt;/code&gt; does a shallow comparison of props, however, the second argument allows you to define a custom equality check function. From React's official docs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* render using props */&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;areEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nextProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/*
  return true if passing nextProps to render would return
  the same result as passing prevProps to render,
  otherwise return false
  */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;areEqual&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if you're looking to do a deep comparison between 2 values and want to take the easy route you can use &lt;code&gt;isEqual&lt;/code&gt; from &lt;code&gt;lodash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's have a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Child.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="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&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&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;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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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="o"&gt;&amp;gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case we have a parent component called &lt;code&gt;App&lt;/code&gt; which takes in a &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rendering...&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="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="na"&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;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you open up &lt;code&gt;Console&lt;/code&gt; you will notice that given each keystroke in the input field the &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; component re-renders. Obviously this doesn't have any performance overhead  at this point in time, but imagine if the &lt;code&gt;Child&lt;/code&gt; component had child components of its own with state. Then you'd trigger a re-render of all components associated with the parent, that'd definitely add overhead to your application.&lt;/p&gt;

&lt;p&gt;To prevent child components from unnecessarily re-rendering like that we have to use &lt;code&gt;React.memo&lt;/code&gt;. All we need to do is wrap our &lt;code&gt;Child&lt;/code&gt; component in our &lt;code&gt;memo&lt;/code&gt; and you see that no matter what we type in the input field it doesn't trigger a re-render of the &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rendering...&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="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="na"&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;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, what if we wanted to pass down functions or anything that isn't a primitive value such as objects since &lt;code&gt;memo&lt;/code&gt; only does a shallow comparison? A shallow comparison in this case means it only checks if the props that you're passing down is referencing the same place in memory. &lt;/p&gt;

&lt;p&gt;So let's say we want to update the &lt;code&gt;counter&lt;/code&gt; from &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; so we do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Child.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateCounterHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="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&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&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;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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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="o"&gt;&amp;gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;updateCounter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updateCounterHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;and within Child.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pushUpdateCounter&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rendering...&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="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="na"&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;counter&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;/strong&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;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;pushUpdateCounter&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;Update&lt;/span&gt; &lt;span class="nx"&gt;Counter&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;However, you will notice that the &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; component still gets rendered whenever we type something into the input field. This is because the &lt;code&gt;updateCounterHandler&lt;/code&gt; inside &lt;code&gt;App.js&lt;/code&gt; gets recreated each time the state changes.&lt;/p&gt;

&lt;p&gt;So the correct way to handle callback functions with memo is by using &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. useCallback
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; is a hook that comes with &lt;code&gt;react&lt;/code&gt; that returns a memoised function. It takes in 2 arguments, the first one being the callback function, the second being an array of dependencies.&lt;/p&gt;

&lt;p&gt;So all that needs to be done is wrap &lt;code&gt;useCallback&lt;/code&gt; around our &lt;code&gt;updateCounterHandler&lt;/code&gt; function to prevent the &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; component from re-rendering whenever we type in the input field.&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;updateCounterHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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;counter&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. useMemo
&lt;/h2&gt;

&lt;p&gt;Like &lt;code&gt;useCallback&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt; is a hook that takes in a function, however, instead of returning a memoised function it returns a memoised value. This makes it useful when performing heavy calculations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;updateCounter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pushUpdateCounter&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rendering...&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;outputNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;output&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;10000000&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;output&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="na"&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;counter&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;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="na"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;outputNumber&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="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;pushUpdateCounter&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;Update&lt;/span&gt; &lt;span class="nx"&gt;Counter&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;useMemo&lt;/code&gt; in the example above, we're able to cache the return value of &lt;code&gt;outputNumber&lt;/code&gt;, so that we're not recalling the function each time.&lt;/p&gt;




&lt;p&gt;After learning these techniques, I hope you're able to apply it to where it's truly needed, because &lt;strong&gt;premature optimisation is the root of all evil!&lt;/strong&gt; It's about finding the fine line between compromising space and time as speed optimisation techniques such as memoisation eat up space (RAM) in return for a faster time. So always question yourself before optimising your code, "do the performance gains really justify the usage?". &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Tools to optimise the performance of your React app</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Mon, 08 Mar 2021 10:55:40 +0000</pubDate>
      <link>https://dev.to/parkrooben/tools-to-optimise-the-performance-of-your-react-app-kil</link>
      <guid>https://dev.to/parkrooben/tools-to-optimise-the-performance-of-your-react-app-kil</guid>
      <description>&lt;p&gt;Although React provides many optimisations for your code out of the box, it is important to nonetheless properly evaluate and optimise the performance of your React app.&lt;/p&gt;

&lt;p&gt;I've found these tools to be most helpful during the development process to help optimise an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Lighthouse
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru2zkrvaekcx4evl5yey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru2zkrvaekcx4evl5yey.png" alt="Alt Text" width="672" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right click on a page and click &lt;code&gt;Inspect&lt;/code&gt; to open developer tools, click on the &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; and you will find &lt;code&gt;Lighthouse&lt;/code&gt; if it doesn't already show in the tab.&lt;/p&gt;

&lt;p&gt;Lighthouse will generate a report of that website by assessing in &lt;code&gt;Performance&lt;/code&gt;, &lt;code&gt;Accessibility&lt;/code&gt;, &lt;code&gt;Best Practices&lt;/code&gt; and &lt;code&gt;SEO&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvp3ppbgi08xkdj8olrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvp3ppbgi08xkdj8olrs.png" alt="Alt Text" width="605" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Profiler
&lt;/h2&gt;

&lt;p&gt;Prior to using Profiler you must have installed &lt;code&gt;React Developer Tools&lt;/code&gt; from the Chrome web store as it is a dev tools extension.&lt;/p&gt;

&lt;p&gt;Once you have that installed you should be able to see Profiler in your developer tools. Close and reopen your browser if you don't see it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkri3i0q59g2fbotzykn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkri3i0q59g2fbotzykn.png" alt="Alt Text" width="466" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get started, hit the &lt;code&gt;record&lt;/code&gt; icon and make some changes on the page and hit the &lt;code&gt;record&lt;/code&gt; icon again to stop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4sdppt1d69mfxtccyi5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4sdppt1d69mfxtccyi5.png" alt="Alt Text" width="606" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Profiler will give you a break down of all Components rendered on the page and how long each one is taking to render. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. Webpack Bundle Analyzer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ebznhrhdqqlzg3pur7t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ebznhrhdqqlzg3pur7t.png" alt="Alt Text" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Webpack Bundle Analyzer is a tool that generates an interactive treemap visualisation of the contents of your bundle. &lt;/p&gt;

&lt;p&gt;You will be able to identify dependencies that take up a significant amount of storage. In the example above, the entire &lt;code&gt;lodash&lt;/code&gt; library was imported for a project. To reduce the bundle size we can introduce &lt;strong&gt;tree shaking&lt;/strong&gt; on the &lt;code&gt;lodash&lt;/code&gt; package. Essentially, only installing the functions that we need.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lodash&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;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;omittedObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;omit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&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;omittedObj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;omit&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lodash/omit&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;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;omittedObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;omit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&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;omittedObj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Thank you for reading! I hope that you will incorporate these technologies and practices into your projects as it will definitely help you write sustainable and reliable code!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Show, Don't Tell: A Story of Management.</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Tue, 03 Dec 2019 04:11:54 +0000</pubDate>
      <link>https://dev.to/parkrooben/show-don-t-tell-a-story-of-management-20h8</link>
      <guid>https://dev.to/parkrooben/show-don-t-tell-a-story-of-management-20h8</guid>
      <description>&lt;p&gt;If you are a senior developer, it is your responsibility to ensure that your team members stay on track to complete their assigned tasks, whilst also being in a positive and encouraging state of mind.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Actions speak louder than words.&lt;/em&gt; We've all heard this quote before, but how many of us actually take this seriously?&lt;/p&gt;

&lt;p&gt;We've all met someone who makes promises without any intention of keeping it, in fact you've mostly likely unintentionally done the same.&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Jerry is a member of your development team. A junior developer with a lot of promise, during meetings he always contributes amazing ideas and brings forth great energy; however, when asked to do solid work, he always slacks off, coming to work as late as possible, and leaving on the dot. You're aware that Jerry has the potential to do amazing things, but his actions aren't mirroring his words.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Why is that?&lt;/p&gt;

&lt;p&gt;There definitely can be something happening in his personal life that is causing this disconnect. However, let's assume for the sake of this article that Jerry consistently shows this type of behaviour for weeks and months on end.&lt;/p&gt;

&lt;p&gt;Why is that?&lt;/p&gt;

&lt;h1&gt;
  
  
  The cause
&lt;/h1&gt;

&lt;p&gt;I'm here to share the (possibly controversial) opinion that &lt;strong&gt;Jerry isn't lazy. In fact, no one is lazy.&lt;/strong&gt; The only reason he appears lazy (i.e. there is a disconnect between his words and his actions) is because he feels comfortable within a company culture that encourages and rewards ideas without action.&lt;/p&gt;

&lt;p&gt;The company culture can involve explicitly stated elements (e.g. "we wear formal attire to work every day"), but in many cases it is implicitly stated (e.g. as the technical lead, by coming in first and leaving last, you're fostering a culture of over-achievement).&lt;/p&gt;

&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;You realise that ideas are commonly being put forth in standup meetings, by Jerry and also by others, but they’re not being objectively evaluated, recorded and assigned accordingly, and that as a result, Jerry isn't realising the importance of delivering features on time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You realise that although Jerry is displaying the most obvious symptoms of this issue, your whole team seems to be living in some degree of disconnection between their words and their actions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You take action and decide to make it a note to include the development team in your next planning meeting with the technical lead, so that they can see the business implications of their actions/non-actions. By doing so, the entire team is able to see what needs to be done.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Specifically for Jerry, you organise some one-on-one planning time each sprint, not only to go through scheduling for his work, but also as a good opportunity to catch up and build rapport. Jerry responds well to this indirect counselling, and his performance quickly improves, making him not only a wonderful fountain of ideas, but also a hard worker who is an invaluable asset to the company.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If everyone is moving forward together, then success takes care of itself."&lt;/p&gt;

&lt;p&gt;– &lt;strong&gt;Henry Ford&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading! What do you guys think? Do you have any similar stories?&lt;/p&gt;

</description>
      <category>career</category>
      <category>leadership</category>
      <category>management</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Twelve-Factor App: How To Embrace The Future</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Sat, 30 Nov 2019 09:17:35 +0000</pubDate>
      <link>https://dev.to/parkrooben/the-twelve-factor-app-how-to-embrace-the-future-20kg</link>
      <guid>https://dev.to/parkrooben/the-twelve-factor-app-how-to-embrace-the-future-20kg</guid>
      <description>&lt;p&gt;In this article, I will explain to you the Twelve-Factor App development guidelines, why it's not a passing fad, and how you can use it to literally (well, figuratively) embrace the future.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Is The Twelve-Factor App?
&lt;/h1&gt;

&lt;p&gt;The Twelve-Factor app is a set of best practices for building modern web applications. There are twelve guidelines (hence the name) that are recommended to be followed by every developer to build scalable applications.&lt;/p&gt;

&lt;p&gt;A Twelve-Factor App is described as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Automated from a development perspective;&lt;/li&gt;
&lt;li&gt;Portable across execution environments;&lt;/li&gt;
&lt;li&gt;Deployable in the cloud minimizing the need for servers and server administration;&lt;/li&gt;
&lt;li&gt;Enabled for continuous deployment with minimal divergence between development and production environments; and&lt;/li&gt;
&lt;li&gt;Scalable without significant change or effort.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What Are The Twelve Factors?
&lt;/h1&gt;

&lt;p&gt;I'll briefly cover each of the factors. However, for an in-depth explanation be sure to check out the official site of The Twelve-Factor App: &lt;a href="https://12factor.net/" rel="noopener noreferrer"&gt;https://12factor.net/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Codebase
&lt;/h2&gt;

&lt;p&gt;Essentially, there will always be only one code repository for a given app. This can be any version control system such as &lt;strong&gt;Git&lt;/strong&gt;, &lt;strong&gt;Mercurial&lt;/strong&gt; or &lt;strong&gt;Subversion&lt;/strong&gt;. There will be many deployments of an app, however, the app can be run in different environments (local/production).&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Dependencies
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;NEVER&lt;/strong&gt; directly copy a dependency into your codebase. You want to use a package manager to explicitly declare and isolate your dependencies. For JavaScript, popular package managers include &lt;em&gt;npm&lt;/em&gt; or &lt;em&gt;yarn&lt;/em&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  3. Config
&lt;/h2&gt;

&lt;p&gt;The config should always be stored in an Environment Variable. You want the config file to be separated away from the code as the config varies throughout deployment and the code does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Backing services
&lt;/h2&gt;

&lt;p&gt;Treat your backing services as attached resources. This means that you want to make your services easily interchangeable so that you can swap to a different third-party service provider without any changes to the app’s code.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Build, release, run
&lt;/h2&gt;

&lt;p&gt;The twelve-factor app uses strict separation between the build, release, and run stages. The build process is run when new code is deployed. Every release should always have a unique release ID, such as a timestamp of the release or an incrementing number. Then you are able to run the release in an execution environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Processes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO NOT&lt;/strong&gt; introduce state in the processes. Twelve-factor processes are stateless and share-nothing. Any data that needs to persist must be stored in a stateful backing service, typically a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Port binding
&lt;/h2&gt;

&lt;p&gt;The web app exports HTTP as a service by binding to a port, and listening to requests coming in on that port. Make sure that you export your services via port binding. The service should be visible to other services as they may be used. &lt;/p&gt;

&lt;h2&gt;
  
  
  8. Concurrency
&lt;/h2&gt;

&lt;p&gt;By separating your app into smaller pieces you are able to scale the services. This reduces the workload as an individual VM can only grow so large (vertical scale), so it is important for the app to span across multiple processes on  multiple physical machines.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Disposability
&lt;/h2&gt;

&lt;p&gt;Processes are disposable, so make sure that they can be started or stopped fast. By doing so this facilitates automatic scaling, rapid and ease of deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Dev/prod parity
&lt;/h2&gt;

&lt;p&gt;Keep development, staging, and production as similar as possible so that anyone working on a project is able to pick it up and release it. Continuous deployment between development and production is imperative to limit failures and errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Logs
&lt;/h2&gt;

&lt;p&gt;Treat logs as event streams. Logs are useful for checking up on your apps processes and backing services. You should never concern the routing or storage of its output stream with your app. Instead, think of these logs as having no fixed beginning or end but flows on a continuous stream as long as the app is running.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Admin processes
&lt;/h2&gt;

&lt;p&gt;Run admin/management tasks as one-off processes for tasks like database migrations and executing one-time scripts in the environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Now?
&lt;/h1&gt;

&lt;p&gt;Regardless of your framework and tech stack, you can implement Twelve-Factor App principles into your application! Some great resources that naturally complement the Twelve-Factor App's principles include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/dotenv" rel="noopener noreferrer"&gt;Dotenv&lt;/a&gt; for configuring environments (the link is for Node.js but different versions are available for different languages and frameworks)&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; for setting up containers and creating perfect development/deployment environments effortlessly;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kubernetes.io/" rel="noopener noreferrer"&gt;Kubernetes&lt;/a&gt; for managing and scaling containers for production-grade uses;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aws.amazon.com/" rel="noopener noreferrer"&gt;AWS&lt;/a&gt; for hosting a deployed Twelve-Factor App; and&lt;/li&gt;
&lt;li&gt;So many more!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This above list is by no means exhaustive. I'll end this article encouraging you to do your own research and find more tools that you can incorporate into your stack. Like it or not, these Twelve-Factor App principles are becoming more and more common, so the best time to get started is now!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
      <category>career</category>
      <category>productivity</category>
    </item>
    <item>
      <title>When should you ask for help? Tips from a senior developer.</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Tue, 26 Nov 2019 01:35:49 +0000</pubDate>
      <link>https://dev.to/parkrooben/when-should-you-ask-for-help-tips-from-a-senior-developer-2mph</link>
      <guid>https://dev.to/parkrooben/when-should-you-ask-for-help-tips-from-a-senior-developer-2mph</guid>
      <description>&lt;p&gt;As a senior JavaScript developer, I've worked with and mentored many developers; and over all these years, there are two types of people that bother me the most.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The ones that ask too many questions.&lt;/strong&gt; &lt;em&gt;e.g. How do you loop through an array? How do you use a reducer?&lt;/em&gt; Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime. I'd much rather junior developers spend 30 minutes researching a problem and finding a solution, and learning self-sufficiency skills along the way, than I help them fix it in 3 minutes time, and they don't have half a clue what's going on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The ones that ask too few questions.&lt;/strong&gt; Afraid of being scrutinised and/or looking to protect their self-esteem, some developers will persist even when they sense something isn't right. Thousands of lines of code later, you end up spending an exponential amount of time understanding and refactoring their mess. What started with a single instance of inheritance on class-based React components then turns into a full-scale "inheritance suite" using poorly-designed OOP models to poorly deliver a solution in "React".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To be fair, it is definitely quite daunting to ask for help. You feel like you're being judged for not knowing something, and you're afraid of asking a stupid question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, when should I ask for help?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://whathaveyoutried.com" rel="noopener noreferrer"&gt;whathaveyoutried.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(^ have a read)&lt;/p&gt;

&lt;p&gt;Here's a procedure I share with others to help them determine when to ask for help:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Can you find/derive a high-quality solution from the internet?&lt;/strong&gt; If your problem is something reasonably small, e.g. &lt;em&gt;how to loop through an array in JavaScript&lt;/em&gt;, then searching on the internet will likely give you a high-quality answer. If your problem is something quite large, e.g. &lt;em&gt;how to build an authentication endpoint using Express and MySQL&lt;/em&gt;, it's very likely that (1) you won't find a solution specific to your needs, and/or (2) the solution(s) you do find are not high-quality. If you cannot find or derive a high-quality solution from the internet, it might be worth asking for help.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Are you hardstuck?&lt;/strong&gt; If you've tried searching on the internet to no avail, it might be worth asking for help.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Are you working in unknown territory?&lt;/strong&gt; If you're working on &lt;strong&gt;anything&lt;/strong&gt; that you're unsure of, it's usually a good idea to ask a more senior developer to sense-check your work, just to make sure you're on the right page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The best junior programmers I've worked with are always the ones who are not afraid to reach out to their senior colleagues for advice, and the best senior developers I've worked with were always willing to mentor their junior colleagues. Any half-decent developer should be willing to help you, so don't be afraid to reach out! However, make sure that your question is specific and shows research effort.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If you do not know how to ask the right question, you discover nothing."&lt;/p&gt;

&lt;p&gt;– W. Edwards Deming&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading! What do you guys think?&lt;/p&gt;

</description>
      <category>career</category>
      <category>leadership</category>
      <category>management</category>
    </item>
    <item>
      <title>How to deal with stubborn employees. (as a senior developer)</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Sun, 24 Nov 2019 03:55:21 +0000</pubDate>
      <link>https://dev.to/parkrooben/how-to-deal-with-stubborn-employees-as-a-senior-developer-4836</link>
      <guid>https://dev.to/parkrooben/how-to-deal-with-stubborn-employees-as-a-senior-developer-4836</guid>
      <description>&lt;p&gt;Everyone knows someone stubborn.&lt;/p&gt;

&lt;p&gt;If you don't, I have some bad news for you. :)&lt;/p&gt;

&lt;p&gt;Regardless of how wrong or right you think you are, if you're not willing to acknowledge others' opinions and compromise as appropriate, you're going to have a hard time getting things done.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Talent wins games, but teamwork and intelligence wins championships."&lt;/p&gt;

&lt;p&gt;– Michael Jordan&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a senior developer / manager, you might have to deal with stubborn employees, or &lt;strong&gt;you might be the stubborn one&lt;/strong&gt;. Either way, not properly addressing the issue can greatly affect your team dynamic.&lt;/p&gt;

&lt;p&gt;Let's look at this scenario:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You are the team lead at a company, and Jerry is a hardworking frontend developer that has been working at this company for 10 years. Recently, the company hired a senior frontend developer to take lead of the development process. As time goes on, Jerry becomes increasingly passive-aggressive, thinking that he is more suited for the job than this senior developer. He now refuses to listen to anyone in his team except the team lead, causing a lot of development blocks and causing many workflow issues.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As the team lead, you realise that if this continues to drag on, this will be very detrimental to the entire team. So you decide to come up with a plan to deal with this type of behaviour.&lt;/p&gt;

&lt;p&gt;Although it may be tempting to call Jerry out in front of the whole team for his attitude, this may cause more problems than it solves.&lt;/p&gt;

&lt;p&gt;At the end of the day, Jerry is still a human being, and human beings have feelings. It's not in your best interest to bring others down like that.&lt;/p&gt;

&lt;p&gt;Instead, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conducting code reviews within a team meeting or group setting where you (as the team lead) are present. This may help to keep his attitude in-check, whilst also ensuring that necessary quality control is completed.&lt;/li&gt;
&lt;li&gt;Orchestrating pair programming sessions to further encourage teamwork. This may help the issue and associated resentment to organically dissolve.&lt;/li&gt;
&lt;li&gt;Talking to Jerry personally, and asking him if something's bothering him. In many cases, it is a combination of various issues and factors that causes behavioural symptoms, and where the employee is willing to discuss and open up, this can be a suitable method of encouragement and counselling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feedback is very important, and it is important that employees correctly perceive their role within the team. In the case of Jerry, by respecting emotional/professional boundaries whilst also being firm on code quality and delivery, you will slowly gain the respect of all your employees, including Jerry.&lt;/p&gt;

&lt;p&gt;Thanks for reading! What do you guys think? How would you handle this kind of situation?&lt;/p&gt;

</description>
      <category>career</category>
      <category>leadership</category>
      <category>management</category>
    </item>
    <item>
      <title>Wait, but what exactly is React?</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Fri, 22 Nov 2019 03:37:57 +0000</pubDate>
      <link>https://dev.to/parkrooben/wait-but-what-exactly-is-react-34ea</link>
      <guid>https://dev.to/parkrooben/wait-but-what-exactly-is-react-34ea</guid>
      <description>&lt;p&gt;​Coworkers and colleagues have been asking me about React since 2014. Instead of explaining it time and time again, I will give my 5-minute summary here in this article.&lt;br&gt;
​&lt;br&gt;
&lt;strong&gt;What is React?&lt;/strong&gt;&lt;br&gt;
​&lt;br&gt;
React is a JavaScript library developed by Jordan Walke, a software engineer at Facebook back in 2011. Since then, React has become a widely popular open-source library that is used by Facebook, Instagram, Netflix, PayPal (just to name a few) and maintained by Facebook, Instagram and an amazing community of developers that have taken an interest in the library.&lt;br&gt;
​&lt;br&gt;
&lt;strong&gt;How does React work?&lt;/strong&gt;&lt;br&gt;
​&lt;br&gt;
React uses a component based architecture (i.e. it involves you writing "components"), defined using a special JSX syntax which compiles down into regular JavaScript. Here we have a &lt;code&gt;Button&lt;/code&gt; component that renders a different class name to the &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; based on the props.&lt;br&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;Button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&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;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`button &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&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="err"&gt;​&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="p"&gt;}&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;props&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&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;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
This level of flexibility makes creating interactive UIs fun as you are able to reuse components in different parts of your webpage.&lt;br&gt;
​&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Although the code within the &lt;code&gt;return&lt;/code&gt; looks like valid HTML, it JSX - a syntax extension to JavaScript to make defining React elements easier.&lt;br&gt;
​&lt;br&gt;
When you want to render a particular kind of button, you just pass the &lt;code&gt;&amp;lt;Button&amp;gt;&lt;/code&gt; component a list of props (similar to attributes in HTML):&lt;br&gt;
​&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleAPICall&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"primary"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
&lt;strong&gt;Why should I learn React?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It's incredibly popular, and as such is very employable.&lt;/strong&gt; A survey conducted by Stack Overflow showed that React was the most loved and wanted web framework.
​
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foh40mbrypxwjq8i1nsw9.png" alt="Alt Text" width="800" height="577"&gt;
​&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's incredibly fun.&lt;/strong&gt; IMHO what makes React so great and so popular is the fact that it is not restrictive. It does so much yet has so few constraints, that using it feels just like magic.&lt;/li&gt;
&lt;li&gt;Do you have another reason? Let's discuss it below in the comments!
​
Thanks for reading!&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Don’t excessively mutate the DOM. Here’s what you should do instead.</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Wed, 20 Nov 2019 11:26:22 +0000</pubDate>
      <link>https://dev.to/parkrooben/don-t-excessively-mutate-the-dom-here-s-what-you-should-do-instead-2d3m</link>
      <guid>https://dev.to/parkrooben/don-t-excessively-mutate-the-dom-here-s-what-you-should-do-instead-2d3m</guid>
      <description>&lt;p&gt;Before I continue I just want to say that it’s not bad practice to target the DOM. Without targeting the DOM, JavaScript wouldn’t be able to manipulate anything on your page, rendering it useless as a frontend programming language.&lt;/p&gt;

&lt;p&gt;However, you do want to stay cautious and be very careful about when and where you’re targeting the DOM, as certain operations may noticeably affect the performance of your webpage.&lt;/p&gt;

&lt;p&gt;Now with that said, you might be asking yourself:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"How bad can directly manipulating the DOM really get?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REALLY REALLY MESSY&lt;/strong&gt; especially if you're working in a team, you're adding unnecessary levels of complexity which can result to potential bugs whether it'd be from performance, testing and even refactoring. &lt;/p&gt;

&lt;p&gt;Take this bit of code for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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;10000&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&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="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&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;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're generating 10,000 &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags and appending each one to the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; of the DOM. This is highly inefficient as this code tells the DOM to update and figure out where everything goes again for 10,000 times.&lt;/p&gt;

&lt;p&gt;A cleaner approach:&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;fragment&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;createDocumentFragment&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;10000&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&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="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By grouping all our elements so that we append them all together we are able to drastically reduce the amount of times that the DOM needs to be modified down to one. This also reduced the total running time from &lt;strong&gt;413.125ms&lt;/strong&gt; to &lt;strong&gt;12.189ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fun Fact:&lt;/strong&gt; React.js provides a VirtualDOM that creates a lightweight copy of the DOM which it keeps track of. React also tries to update the DOM as little as possible by grouping together changes.&lt;/p&gt;

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

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The window, the mirror, and how a little bit of introspection can go a long way</title>
      <dc:creator>Benjamin Liu</dc:creator>
      <pubDate>Mon, 18 Nov 2019 09:59:57 +0000</pubDate>
      <link>https://dev.to/parkrooben/the-window-the-mirror-and-how-a-little-bit-of-introspection-can-go-a-long-way-3455</link>
      <guid>https://dev.to/parkrooben/the-window-the-mirror-and-how-a-little-bit-of-introspection-can-go-a-long-way-3455</guid>
      <description>&lt;p&gt;Picture this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Your team deployed the company's flagship project just 2 hours before the investor meeting. Now the demo's started - no more last-minute changes are possible. The only thing piercing the silence is the steady drip-drip of a leaky faucet on the floor above. Everyone is trying their best to eavesdrop in on the conversation going on in the conference room, where the CEO is demonstrating the project to the group of investors.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Oops - that wasn't supposed to happen."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Your heart sinks. The demo just went horribly wrong. As the team lead, you know that this whole thing was ultimately your responsibility.&lt;/p&gt;

&lt;p&gt;Now in this moment, you can do one of two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;You can blame it on Jerry.&lt;/strong&gt; Yes, it must be Jerry's fault! In fact, we have clear evidence that Jerry wrote the code that ended up breaking. Yes, it must be Jerry's fault! (Note: &lt;em&gt;poor Jerry is a metaphor for literally anyone else on your team.&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can take the blame and handle it gracefully.&lt;/strong&gt; Either way, the company has lost a huge opportunity, and as the team lead you are responsible for the project. Best not to let the resentment and guilt seep any further down.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You decide to go with option 2.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Yes, it was me. I should've had the foresight to include additional testing as part of our last sprint, and I failed to do that. I'm sorry."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Your team sits in silence as the CEO continues to rant for what seemed like an eternity.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the next sprint the following Monday, just as you were about to speak, Jerry interjects:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"I just wanted to say thanks for taking the blame on behalf of the team. I know that it was my code that broke, and I apologise. I've already added that issue as a bug onto our issues board."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Others in the team chime in too:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Yeah, what you did was really brave and totally undeserved."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Thanks for sticking up for us."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Imagine a window and a mirror on the wall in front of you.&lt;/p&gt;

&lt;p&gt;When something goes wrong, a bad leader would look out the window and blame the people out there - their own team, staff from other teams, third parties - anyone but themselves. However, a good leader would look into the mirror and try to identify their own problems, and how they improve.&lt;/p&gt;

&lt;p&gt;When something goes right, a bad leader would look into the mirror and praise themselves - their leadership skills, their technical prowess, their networking ability. On the other hand, a good leader would look out the window, and share the praise amongst the team.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Individual commitment to a group effort - that is what makes a team work, a company work, a society work, a civilization work."&lt;/p&gt;

&lt;p&gt;– &lt;strong&gt;Vince Lombardi&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading! What do you guys think?&lt;/p&gt;

</description>
      <category>career</category>
      <category>leadership</category>
      <category>management</category>
    </item>
  </channel>
</rss>
