<?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: thepurplecoder</title>
    <description>The latest articles on DEV Community by thepurplecoder (@thepurplecoder).</description>
    <link>https://dev.to/thepurplecoder</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%2F429863%2F699f0e69-0914-4d75-b7a2-46c4845421f4.jpg</url>
      <title>DEV Community: thepurplecoder</title>
      <link>https://dev.to/thepurplecoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thepurplecoder"/>
    <language>en</language>
    <item>
      <title>Why react stopped using stack reconciler? - Blog 2</title>
      <dc:creator>thepurplecoder</dc:creator>
      <pubDate>Sat, 19 Jun 2021 15:42:55 +0000</pubDate>
      <link>https://dev.to/thepurplecoder/why-react-stopped-using-stack-reconciler-blog-2-171p</link>
      <guid>https://dev.to/thepurplecoder/why-react-stopped-using-stack-reconciler-blog-2-171p</guid>
      <description>&lt;p&gt;In the  &lt;a href="https://dev.to/thepurplecoder/the-basics-of-react-internals-blog-1-3md7"&gt;previous blog&lt;/a&gt; I've written about how React detects a change when an app moves from one state to another. Now let's implement a simple naive recursive algorithm using those concepts and analyse the drawbacks of using recursion for reconciliation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Structure of Virtual DOM - Naive algorithm
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that the following algorithm is just to give you a gist of how React 15 worked before in a simple way. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We all know the real DOM follows a  &lt;a href="https://en.wikipedia.org/wiki/Tree_data_structure"&gt;tree&lt;/a&gt; data structure and each  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element"&gt;node&lt;/a&gt; has a property called &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/children"&gt;children&lt;/a&gt; which contains all of the references of the child elements. &lt;/p&gt;

&lt;p&gt;Let's follow the same tree structure for each node of our virtual dom as Virtual dom is just a clone of real dom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type VirtualElement{
   type: string | object,
   /* 
             If the element is a host component like div type will be a string('div')   
             If the element is a custom react element type is the reference of class/function
  */
   props: any,
   children: Array&amp;lt;element&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When an element is created via React.createElement we will attach the element to our virtual dom by attaching the element to its parent node as a child. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;div&amp;gt; &amp;lt;span&amp;gt; Hello Virtual DOM &amp;lt;span/&amp;gt; &amp;lt;/div&amp;gt;, rootEle);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The virtual dom of the above code will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  type: 'h1',
  children: [
    {
      type: 'span',
      children: ['Hello Virtual DOM']
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Please note that I've omitted most of the keys like ref and key for simplicity. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Steps of the naive algorithm
&lt;/h2&gt;

&lt;p&gt;Now that we've designed our virtual dom structure let's discuss the steps of the naive algorithm using the below code as an example. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The below example will be considered as a reference throughout this article&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class App extends Component{
    state = {
        message: 'Hello'
    }
    onChange = (e) =&amp;gt; {
        this.setState({message: e.target.value });
    }
    render(){
        const { message } = this.state;
        return(
            &amp;lt;ul&amp;gt;
                &amp;lt;li&amp;gt; 
                       &amp;lt;span&amp;gt;${message}01&amp;lt;/span&amp;gt; 
                       &amp;lt;span&amp;gt;${message}02&amp;lt;span&amp;gt; 
                       &amp;lt;span&amp;gt;${message}03&amp;lt;span&amp;gt;  
                &amp;lt;/li&amp;gt;
                 &amp;lt;li&amp;gt; 
                       &amp;lt;span&amp;gt;${message}11&amp;lt;/span&amp;gt; 
                       &amp;lt;span&amp;gt;${message}12&amp;lt;span&amp;gt; 
                       &amp;lt;span&amp;gt;${message}13&amp;lt;span&amp;gt;  
                &amp;lt;/li&amp;gt;
                 &amp;lt;input value={message} onChange={this.onChange}/&amp;gt;
            &amp;lt;/ul&amp;gt;
        )
    }
}

ReactDOM.render(&amp;lt;App/&amp;gt; , rootEle);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Structural representation of virtual dom for the above code&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Rb-wfV5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3khdpo7ho48zuxpzv7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Rb-wfV5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3khdpo7ho48zuxpzv7b.png" alt="Virtual DOM for the above code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm - Steps&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When ReactDOM.render is called for the first time we will create a virtual DOM by iterating the first element i.e &lt;strong&gt;App&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;While creating virtual dom we will create the corresponding DOM nodes and append the element to its corresponding parent.&lt;/li&gt;
&lt;li&gt;Whenever any state changes through &lt;em&gt;setState&lt;/em&gt; we will mark it as dirty and pass it to our reconcile function. &lt;/li&gt;
&lt;li&gt; Reconcile function accepts &lt;em&gt;currentNode&lt;/em&gt; as a param and recursively reconcile every element present in the &lt;em&gt;currentNode&lt;/em&gt; tree to find the change and update the corresponding change in DOM too.&lt;/li&gt;
&lt;li&gt;If the current node is changed/added/deleted because of an update we will change its attribute or delete or add the node to our virtual dom and real dom.
&lt;code&gt;shouldComponentUpdate&lt;/code&gt; or &lt;code&gt;React.memo&lt;/code&gt; or &lt;code&gt;PureComponent&lt;/code&gt; checks will be handled in this step.&lt;/li&gt;
&lt;li&gt;Get children of the &lt;em&gt;currentNode&lt;/em&gt; by calling it's &lt;em&gt;render&lt;/em&gt; method if it is a class component or &lt;em&gt;currentNode(props)&lt;/em&gt; if it is a function component with updated props. &lt;/li&gt;
&lt;li&gt;Iterate through every child of &lt;em&gt;currentNode&lt;/em&gt; and go to step 5 for reconciling every child.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  FlowChart
&lt;/h3&gt;

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

&lt;p&gt;The &lt;a href="https://github.com/facebook/react/tree/15-stable/src/renderers/shared/stack/reconciler"&gt;algorithm&lt;/a&gt;  for React 15 and its previous versions works almost the same as the above that we've discussed though React15 implements more concepts like  &lt;a href="https://github.com/facebook/react/blob/15-stable/src/renderers/shared/stack/reconciler/ReactDefaultBatchingStrategy.js"&gt;Batching&lt;/a&gt;  etc...&lt;br&gt;
Since it relies on recursion which uses  &lt;a href="https://en.wikipedia.org/wiki/Call_stack"&gt;call stack&lt;/a&gt; to track the currently processing node, we call this as &lt;em&gt;Stack Reconciler&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack calls for the recursive algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sKgk-GCQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arlnfozvvig9kcmya4ec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sKgk-GCQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arlnfozvvig9kcmya4ec.png" alt="Stack calls for the recursive algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Drawbacks of stack reconciler.
&lt;/h2&gt;

&lt;p&gt;Let's imagine that in our &lt;em&gt;App&lt;/em&gt; there are 1000 &lt;em&gt;li&lt;/em&gt; items and each item takes at least 1000 ms to reconcile(render). Now our main thread will be stuck for 1000 seconds for processing each update. If the user types something, it will process the update only after finishing the current update. The main thread is spending more time on low priority tasks like updating &lt;em&gt;li&lt;/em&gt; items rather than a high priority update that users can easily perceive if there is a delay. &lt;/p&gt;

&lt;p&gt;We can solve this by synchronously performing high priority task and then incrementally perform low priority task by scheduling them using requestIdleCallback or Task Queue. Before we start processing the next node in a low priority update we will check if we reached the deadline. If there is still time remaining we will process that node and if there is no time remaining we yield our task or empty the call stack so that the main thread can process some other important updates and schedule the next update in the next frame.&lt;/p&gt;

&lt;p&gt;Notice that in a low priority update we have to interrupt the stack when the deadline is passed and have to resume the update in the next frame. In our recursive algorithm if we empty the stack in the middle of reconciling we will lose track of our updates and the nodes that are already processed. &lt;/p&gt;

&lt;p&gt;We can save our progress in a variable to keep track of it, but whenever we interrupt and process the next update in the next frame we have to rebuild the stack in that little amount of time (16ms)which is not idle for an efficient UI library. That's why react team modified their virtual DOM structure in React 16 so that it doesn't couple to JS stack and also makes it easier to interrupt the reconciliation process. &lt;/p&gt;

&lt;p&gt;In the next article, we will learn about &lt;strong&gt;Fiber&lt;/strong&gt; which is used in React 16 which is easily interruptible while reconciling in an optimized manner.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React 16 or higher modifies the virtual DOM structure only so that it is interruptible if we enabled the experimental feature &lt;a href="https://reactjs.org/docs/concurrent-mode-patterns.html#splitting-high-and-low-priority-state"&gt;concurrent mode&lt;/a&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>The basics of React internals - Blog 1</title>
      <dc:creator>thepurplecoder</dc:creator>
      <pubDate>Sun, 13 Jun 2021 16:42:49 +0000</pubDate>
      <link>https://dev.to/thepurplecoder/the-basics-of-react-internals-blog-1-3md7</link>
      <guid>https://dev.to/thepurplecoder/the-basics-of-react-internals-blog-1-3md7</guid>
      <description>&lt;p&gt;React is one of the most popular javascript libraries for building user interfaces. The main advantage of react is when an update happens it only updates the DOM elements that need to be updated. It achieves this by using  &lt;a href="https://reactjs.org/docs/faq-internals.html" rel="noopener noreferrer"&gt;virtual DOM&lt;/a&gt;  which is nothing but an internal representation of our UI. &lt;/p&gt;

&lt;p&gt;Let’s try to understand how react manages virtual dom and how it evolves the virtual dom structure to support concurrent version. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;For those of you who just want to get a gist of react internals I highly recommend to watch  &lt;a href="https://twitter.com/linclark" rel="noopener noreferrer"&gt;Lin Clark&lt;/a&gt; 's &lt;a href="https://www.youtube.com/watch?v=ZCuYPiUIONs" rel="noopener noreferrer"&gt;video of react fiber&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's start with the basics. &lt;/p&gt;

&lt;h3&gt;
  
  
  JSX
&lt;/h3&gt;

&lt;p&gt;React introduces &lt;a href="https://babeljs.io/docs/en/babel-plugin-transform-react-jsx" rel="noopener noreferrer"&gt;JSX&lt;/a&gt; for better developer experience. JSX  is a syntactic sugar for React.createElement(type, props,...children) or _jsx/ _jsxs &lt;/p&gt;

&lt;p&gt;For example when we define a component like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbmvu83g81k49rsbntq1.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%2Fhbmvu83g81k49rsbntq1.png" alt="In"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the babel compiler transpiles the above code to &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjc6k49zotbp1hcbzcqf7.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%2Fjc6k49zotbp1hcbzcqf7.png" alt="Out - React classic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And in current versions *_jsx&lt;/em&gt; will be there instead of &lt;em&gt;React.createElement&lt;/em&gt;. Both performs the same. _jsx was created for improving our React development experience.*&lt;/p&gt;

&lt;p&gt;React.createElement returns a JS object describing the node and its properties. &lt;br&gt;
Eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const profile = &amp;lt;h1 className='welcome'&amp;gt;Hello World &amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would return something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  type: 'h1',
  props: {
     className: 'welcome',
     children: ['Hello World']
  },
  ref: null,
  key: null,
  $$typeof: Symbol(react.element)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The returned element has the following properties.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;type&lt;/strong&gt;&lt;br&gt;
The type of the element &lt;br&gt;
If it is a custom react element the type would be the reference of the function/class of the element. &lt;br&gt;
If it is a host element(like div in DOM) the type would be a string one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;props&lt;/strong&gt;&lt;br&gt;
The props of the element like its children or colour etc.,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$$typeof&lt;/strong&gt;&lt;br&gt;
This key is just to ensure the component is created from react to prevent XSS. &lt;a href="https://twitter.com/dan_abramov" rel="noopener noreferrer"&gt;Dan Abramov&lt;/a&gt; has explained the importance of key in  &lt;a href="https://overreacted.io/why-do-react-elements-have-typeof-property/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;. Do check that out. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;key&lt;/strong&gt;&lt;br&gt;
It is used to uniquely identify the element in case the type of the element is not enough to identify it uniquely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ref&lt;/strong&gt;&lt;br&gt;
Reference of the instance if it is a custom react element or the dom node reference if it is a host element&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using this object builds a tree-like data structure i.e virtual DOM and stores it in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reconciliation
&lt;/h3&gt;

&lt;p&gt;When an update occurs react uses the previously constructed virtual dom and generates a new tree based on the update. React compares these two versions and updates the elements in the DOM efficiently. This process is called  &lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;Reconciliation&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;But if react actually compares every node/object in the tree with its previous version, it'd be time-consuming right?. To avoid this react uses a heuristic algorithm that is based on the following rules.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Elements of different types will generate different branches and subtrees&lt;/em&gt;&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt; Hi I am a div &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the above element changes to the following element,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;span&amp;gt; Hi I am a span &amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then react will delete the div and appends span into its parent. Similarly, if&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Hello/&amp;gt;

/* Changes to */

&amp;lt;Bye/&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Then react will unmount &lt;em&gt;Hello&lt;/em&gt; element or its instance and mounts &lt;em&gt;Bye&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Elements of the same type&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If the type of both the elements is the same it will check whether the props changed. If it is changed it will then modify the props of the element. Simple! &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Message text='Hi'/&amp;gt; ==&amp;gt; &amp;lt;Message text='Bye'/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will modify the text property from 'Hi' to 'Bye' in the &lt;em&gt;Message&lt;/em&gt; instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className='active'/&amp;gt; ==&amp;gt; &amp;lt;div className='inactive'/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will modify the class of the &lt;em&gt;div&lt;/em&gt; element &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Elements of the same type but with different properties&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's say we want to list a bunch of fruits like the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li&amp;gt;Banana 🍌&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;Cherry 🍒&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And due to an update, we append an entry to the first child of ul element now our component will look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li&amp;gt;Apple 🍎 &amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;Banana 🍌&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;Cherry 🍒&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since react is working on our above-said rules, react will do the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checks &lt;em&gt;ul&lt;/em&gt; element type first. Since it is the same in both versions it proceeds to check its props. Since children are changed(Reference will be changed) it starts to check every &lt;em&gt;li&lt;/em&gt; element&lt;/li&gt;
&lt;li&gt;Checks the first element of &lt;em&gt;ul&lt;/em&gt; element i.e &lt;em&gt;li&lt;/em&gt; in both version since the text content is changed from &lt;em&gt;Banana 🍌&lt;/em&gt; to &lt;em&gt;Apple 🍎&lt;/em&gt;, it updates the change to the DOM. Then it proceeds to check the next element&lt;/li&gt;
&lt;li&gt;Now it checks the second &lt;em&gt;li&lt;/em&gt; element and updates the text change to the DOM from &lt;em&gt;Cherry 🍒&lt;/em&gt;  to &lt;em&gt;Banana 🍌&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Then it finds out that there is a new element created at the bottom. So it creates a new &lt;em&gt;li&lt;/em&gt; element and appends the element to the &lt;em&gt;ul&lt;/em&gt; element. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Have you noticed anything unusual in this behaviour? &lt;em&gt;Apple&lt;/em&gt;  is the one that was appended to the first but react is updating the other two items unnecessarily, right? i.e Instead of performing a single update, react is performing three updates. &lt;br&gt;
To avoid this react has introduced the &lt;em&gt;key&lt;/em&gt; to uniquely identify each element.  &lt;/p&gt;

&lt;p&gt;Elements of the same type but with different keys will be uniquely identified and updated now. So if we introduce &lt;em&gt;key&lt;/em&gt; to the above example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li key={1}&amp;gt;Banana 🍌&amp;lt;/li&amp;gt;
  &amp;lt;li key={2}&amp;gt;Cherry 🍒&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
  &amp;lt;li key={0}&amp;gt;Apple 🍎 &amp;lt;/li&amp;gt;
  &amp;lt;li key={1}&amp;gt;Banana 🍌&amp;lt;/li&amp;gt;
  &amp;lt;li key={2}&amp;gt;Cherry 🍒&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now react while comparing the first element knows that &lt;em&gt;Apple 🍎&lt;/em&gt; is newly added to the list and performs that single update. &lt;/p&gt;

&lt;p&gt;Please do note that using &lt;em&gt;index&lt;/em&gt; as &lt;em&gt;key&lt;/em&gt; may create some issues especially when each of your components has an internal state and reorders between list is pretty common. Check this  &lt;a href="https://codepen.io/pen?&amp;amp;editors=0010" rel="noopener noreferrer"&gt;codepen&lt;/a&gt; for more details. &lt;/p&gt;

&lt;p&gt;In the next article, we will try to create a naive react algorithm or the algorithm which relies on recursion by implementing the above concepts. &lt;/p&gt;

&lt;p&gt;*P.S: This blog-series is largely inspired from  &lt;a href="https://indepth.dev/posts/1008/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react" rel="noopener noreferrer"&gt;this amazing blog&lt;/a&gt; written by  &lt;a href="https://twitter.com/maxkoretskyi" rel="noopener noreferrer"&gt;Max Koretskyi&lt;/a&gt;  *&lt;/p&gt;

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