<?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: smhaley</title>
    <description>The latest articles on DEV Community by smhaley (@smhaley).</description>
    <link>https://dev.to/smhaley</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%2F550375%2F92689ceb-6c6a-46fe-8ce2-c2e1d71f3072.jpeg</url>
      <title>DEV Community: smhaley</title>
      <link>https://dev.to/smhaley</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smhaley"/>
    <language>en</language>
    <item>
      <title>React Virtual DOM, Diffing, and Keys</title>
      <dc:creator>smhaley</dc:creator>
      <pubDate>Mon, 07 Feb 2022 04:29:33 +0000</pubDate>
      <link>https://dev.to/smhaley/react-virtual-dom-diffing-and-keys-119g</link>
      <guid>https://dev.to/smhaley/react-virtual-dom-diffing-and-keys-119g</guid>
      <description>&lt;p&gt;This post is a quick run through of the React Virtual DOM and it's implications on using the &lt;code&gt;key&lt;/code&gt; prop during development. &lt;/p&gt;

&lt;p&gt;There are many reviews of this topic around the web. However I still see many newer devs make mistakes with keys. I hope my addition with interactive examples will add some clarity to the topic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://smhaley.github.io/key-demo/"&gt;Check out this link for the interactive demos&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The React Virtual DOM and Reconciliation
&lt;/h2&gt;

&lt;p&gt;React is fast. Very fast. Part of its secret sauce is calculating all the changes that occurred (such as state and prop changes) in memory before applying them to the actual browser &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction"&gt;DOM&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;In memory React keeps a virtualized copy of the the DOM. Whenever an event triggers a rerender, React compares the new state of the Virtual DOM with that of  the previous via a diffing algorithm.&lt;/p&gt;

&lt;p&gt;The algorithm then reconciles what has been updated with what has not and updates the browser DOM with all changes in batch. See this process in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sWd8HLvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.geeksforgeeks.org/wp-content/uploads/20210622175445/diagram1-300x153.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sWd8HLvc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.geeksforgeeks.org/wp-content/uploads/20210622175445/diagram1-300x153.PNG" alt="reconciliation" width="300" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Diffing
&lt;/h3&gt;

&lt;p&gt;To understand diffing it helps to think of a React app as a tree:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_p_snaCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95k3oxr7ge33n8ryizkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_p_snaCH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95k3oxr7ge33n8ryizkg.png" alt="diffing" width="689" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The left tree diagram is a React app. The red node is single component (element) updating within the application.&lt;/p&gt;

&lt;p&gt;React then updates all the 'child' elements below the updating element (see the right side). That is, both the red elements are rerendering or possibly remounting as a result of the top level change. &lt;/p&gt;

&lt;h3&gt;
  
  
  How does React decide to rerender or remount?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/reconciliation.html"&gt;This is controlled by the core assumption of diffing&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Two elements of different types will produce different trees.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The developer can hint at which child elements may be stable across different renders with a key prop.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So what does that mean?&lt;/p&gt;

&lt;p&gt;React will fully remount a component when the actual element changes: such as &lt;code&gt;&amp;lt;Component/&amp;gt;&lt;/code&gt; changing to &lt;code&gt;&amp;lt;Component2/&amp;gt;&lt;/code&gt; or a &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; changing to a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is reasonable. If the the component itself is different the diffing process completely unmounts the outdated element and remounts the new element. The catch is, everything below the unmounted element gets unmounted and remounted as well (all state is wiped out of each unmounted element). In this context, the change on the left causes both the red elements on the right to remount. &lt;/p&gt;

&lt;p&gt;But what about rerendering?&lt;/p&gt;

&lt;p&gt;If the diffing algorithm determines that the changes were only attributes on the element (such as props or state) it will only rerender the component that changed and all components below (that is why the image on the right has both elements as red). &lt;/p&gt;

&lt;p&gt;The second assumption allows developers to let React know that a component has changed using the &lt;code&gt;key&lt;/code&gt; prop. The &lt;code&gt;key&lt;/code&gt; prop is often used in lists, but in the context of a component it will force the component to unmount and remount whereas the diffing algorithm was hinted about the change.&lt;/p&gt;

&lt;p&gt;Okay let's look at a demo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R01Pe1Ww--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izkc2ba9xsoymb5wsxuc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R01Pe1Ww--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izkc2ba9xsoymb5wsxuc.gif" alt="key example" width="712" height="708"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above gif there is a Root (blue background), Child (Color Changing), a Deep Child (Pink), and a Tree. Each of these components is represented by the Tree diagram showing the state of mounts, remounts and rerenders.&lt;/p&gt;

&lt;p&gt;When the gif starts all render counts are 1. &lt;br&gt;
As  the user updates the state of the Child component (by paginating), React diffing renders all components within the Child causing the render count to increase. Since there was just an attribute change, there was no remount. &lt;/p&gt;

&lt;p&gt;Updating state at the Root (&lt;strong&gt;Update Element Attribute&lt;/strong&gt; button) causes all the components in the Tree diagram to rerender--increasing the render count. This is because the &lt;strong&gt;Update Element Attribute&lt;/strong&gt; button updates state at the root (color prop) and passes this new prop to the child.&lt;/p&gt;

&lt;p&gt;It is not until the actual &lt;strong&gt;Change Element&lt;/strong&gt; button is selected that the diffing algorithm realizes that the Child and everything below it must be unmounted and remounted. This is because the &lt;strong&gt;Change Element&lt;/strong&gt; button updates a counter state in the Root component and passes this new count to the &lt;code&gt;key&lt;/code&gt; prop of the Child. The diffing algorithm simply rerenders the Root due to the state change, but completely removes all child elements below--wiping all internal state (see the cache data loss).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://smhaley.github.io/key-demo/"&gt;Interactive demo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/smhaley/key-demo/tree/master/src/components/key-demo"&gt;Code for this gif -- key-demo.tsx is Root&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  But what about Keys with lists?
&lt;/h2&gt;

&lt;p&gt;Lists are a special use case for the &lt;code&gt;key&lt;/code&gt; prop. This is because React is fairly inefficient with list element rendering. If a list were to be updated with a new element anywhere other than the bottom, React will mutate each item within that list. To prevent this, React uses the &lt;code&gt;key&lt;/code&gt; prop within lists to track which element is new and which is not. &lt;/p&gt;

&lt;p&gt;For this reason the general wisdom within the community is to never use indices as the key when iterating over a list.&lt;/p&gt;

&lt;p&gt;Doing so will confuse the diffing algorithm to what is actually changing. &lt;/p&gt;

&lt;p&gt;Take a look below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e5hyv7yR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3yv95lagagdobzlqlsn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e5hyv7yR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3yv95lagagdobzlqlsn.gif" alt="list example" width="691" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both the left and right list columns are the same data. The only difference is that the left list keys off an index while the right keys off a locally unique value.&lt;/p&gt;

&lt;p&gt;Once both inputs are checked, the &lt;strong&gt;Add Item&lt;/strong&gt; button is selected. The buttom adds additional elements to the top of the list. &lt;/p&gt;

&lt;p&gt;As a result, the input stays with the index &lt;strong&gt;0&lt;/strong&gt; key on the left, but travels with the properly selected &lt;strong&gt;a&lt;/strong&gt; label on the right. The diffing algorithm does not notice the issue on the left whereas the key has not changed!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://smhaley.github.io/key-demo/"&gt;Interactive demo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/smhaley/key-demo/blob/master/src/components/list-demo.tsx"&gt;Code for this gif&lt;/a&gt;&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>react-text-annotate-blend: a component for blended annotations</title>
      <dc:creator>smhaley</dc:creator>
      <pubDate>Sun, 21 Mar 2021 13:59:41 +0000</pubDate>
      <link>https://dev.to/smhaley/react-text-annotate-blend-a-component-for-blended-annotations-1fhc</link>
      <guid>https://dev.to/smhaley/react-text-annotate-blend-a-component-for-blended-annotations-1fhc</guid>
      <description>&lt;p&gt;&lt;a href="https://smhaley.github.io/react-text-annotate-blend/"&gt;Live Demo&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Recently I was tasked with developing a homegrown tagging tool for NLP model development. &lt;/p&gt;

&lt;p&gt;At first our team looked to some of the more popular tools such as &lt;a href="https://doccano.herokuapp.com/"&gt;Doccano&lt;/a&gt; and &lt;a href="https://prodi.gy/"&gt;prodi.gy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Due to infrastructure challenges we decided to develop our own annotation tool using react. Using the the library &lt;a href="https://www.npmjs.com/package/react-text-annotate"&gt;react-text-annotate&lt;/a&gt; this wasn't too bad. &lt;/p&gt;

&lt;p&gt;Unfortunately the way this tool handles overlapping tags is to duplicate text with visually unappealing results. Looking around at the NLP annotation space, I noticed there were few quick and simple tools for overlapping annotations.  &lt;/p&gt;

&lt;p&gt;The natural question was, can I overlap and blend tags more naturally?&lt;/p&gt;

&lt;p&gt;Spending some time, I was able to come up with &lt;a href="https://smhaley.github.io/react-text-annotate-blend/"&gt;react-text-annotate-blend&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Currently this library offers a single component &lt;b&gt;TextAnnotateBlend&lt;/b&gt;. The component allows for overlapping annotations, resulting in a mixture of the annotation colors. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KwmYqKu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1j2xncnssk1x2piwvo7l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KwmYqKu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1j2xncnssk1x2piwvo7l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out a demo with live code &lt;a href="https://smhaley.github.io/react-text-annotate-blend/"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Let me know what you think! &lt;/p&gt;

&lt;p&gt;One update I may make soon is a stripped blending of the colors using linear gradients. &lt;/p&gt;

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

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Pretty Easy Privacy</title>
      <dc:creator>smhaley</dc:creator>
      <pubDate>Thu, 31 Dec 2020 05:06:54 +0000</pubDate>
      <link>https://dev.to/smhaley/pretty-easy-privacy-54o3</link>
      <guid>https://dev.to/smhaley/pretty-easy-privacy-54o3</guid>
      <description>&lt;h2&gt;
  
  
  PGP Powered Encryption Made Easy
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://prettyeasyprivacy.xyz/#/"&gt;&lt;b&gt;prettyeasyprivacy.xyz&lt;b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty Easy Privacy gives users the power of openPGP encryption without the hassle of the command line. &lt;/p&gt;

&lt;p&gt;The application performs encryption using &lt;a href="https://openpgpjs.org/"&gt;openPGP.js&lt;/a&gt; (maintained by the protonmail folks).&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals
&lt;/h2&gt;

&lt;p&gt;The goal of this project is to offer a simplified, open source, tool to encrypt files/objects for storage.&lt;/p&gt;

&lt;p&gt;With the open nature of this project the hope is more individuals will be encouraged to practice and explore encryption tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  Behind the Scenes
&lt;/h2&gt;

&lt;p&gt;This is a simple react app hosted on github pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Passphrase Encryption Demo
&lt;/h2&gt;

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

&lt;h3&gt;
  
  
  What do you think?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://prettyeasyprivacy.xyz/#/"&gt;&lt;b&gt;prettyeasyprivacy.xyz&lt;b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;

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