{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/understanding-react-rendering/","result":{"data":{"markdownRemark":{"id":"f135d4b5-42c2-5f4f-b0fc-9b224170ec28","excerpt":"Rendering is an essential procedure a programmer has to manage in frontend development. In React, the render() method is the only required method in a class…","html":"<p>Rendering is an essential procedure a programmer has to manage in frontend development. In React, <a href=\"https://reactjs.org/docs/react-component.html#render\">the render() method is the only required method in a class component</a> and is responsible for describing the view to be rendered to the browser window. Coupled with the clever way React operates around its virtual DOM concept, there are certain subtleties in how this method works. Understanding them will greatly benefit any aspiring React developer. </p>\n<p>Throughout this writing, I will reference <a href=\"https://codepen.io/n-nguyen/pen/WNwYrRG\">this codepen</a> for a demonstration of discussed behaviors.</p>\n<h3 id=\"1-strongcoderendercodestrong-101\" style=\"position:relative;\"><a href=\"#1-strongcoderendercodestrong-101\" aria-label=\"1 strongcoderendercodestrong 101 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. <strong><code>render()</code></strong> 101</h3>\n<p>First of all, <strong><code>render()</code></strong> is not user callable. It is part of the <a href=\"https://reactjs.org/docs/state-and-lifecycle.html\">React component lifecycle</a>. Generally, it gets called by React at various app stages when the React component instantiates for the first time, or when there is a new update to the component state. Render does not take any arguments and returns a <strong><code>JSX.Element</code></strong> which contains the view hierarchy of the current component. This view hierarchy will later be translated into HTML and displayed in the browser window.</p>\n<p>As mentioned before, <strong><code>render()</code></strong> is not user callable as it is an event that happens in the component’s lifecycle. With that said, if it is absolutely necessary to render the view manually, you can instead call the built-in class method <strong><code><a href=\"https://reactjs.org/docs/react-component.html#forceupdate\">forceUpdate()</a></code></strong>. Keep in mind that this is considered an <strong>anti-pattern</strong>. If you were designing sensible React components, its state and props changes should naturally control the render process, and you should never feel the need to make a manual call.</p>\n<p>Within the lifecycle, these are the scenarios where render is called: </p>\n<ul>\n<li>After the React component is first instantiated, following the <strong><code>constructor()</code></strong> call.</li>\n<li>After an update to the component’s props</li>\n<li>After a <strong><code>setState()</code></strong> call</li>\n</ul>\n<p>If you have the <a href=\"https://codepen.io/n-nguyen/pen/WNwYrRG\">Codepen</a> opened at this point, before anything is rendered you will see 2 alert messages from the browser: <code>\"render() is called in Parent component!\"</code>, and <code>\"render() is called in Child component!\"</code>. These messages are invoked from the corresponding <strong><code>render()</code></strong> methods of the example's parent and child component. They serve to introduce the first case of <strong><code>render()</code></strong> invocation: when the component is first instantiated.</p>\n<p>Once the set of alerts is dismissed, a very simple UI will render:</p>\n<p><img src=\"/86a7253364a0299e9ca03339073fecfb/image1.webp\" alt=\"Example UI\"></p>\n<p>The dotted border line distinguishes between elements that belong to the Child component of the example (inside the dotted line) versus the Parent component.</p>\n<ul>\n<li>Clicking button 1 will update the <strong><code>childElementText</code></strong> state of the Parent component, which in turns updates the <strong><code>text</code></strong> prop of the Child component, triggering a render in both Parent and Child.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">onChildPropChange</span><span class=\"mtk1\"> = () </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">this</span><span class=\"mtk1\">.</span><span class=\"mtk11\">setState</span><span class=\"mtk1\">({</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk12\">childElementText:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;I am the child element! I am updated following a prop change.&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    })</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  }</span></span></code></pre>\n<ul>\n<li>Clicking button 2 will update the <strong><code>helloWorldMessage</code></strong> state within Child component, triggering a render following a state change.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">onTextChange</span><span class=\"mtk1\"> = () </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">this</span><span class=\"mtk1\">.</span><span class=\"mtk11\">setState</span><span class=\"mtk1\">({</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk12\">helloWorldMessage:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;Hello React! (state change after setState call)&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    })</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  }</span></span></code></pre>\n<p>A visual and interactive reference to the React lifecycle can be found <a href=\"https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/\">here</a>.</p>\n<p>It is worth noting that following a props update or <strong><code>setState()</code></strong>, the method <strong><code>shouldComponentUpdate()</code></strong> is invoked to determine whether <strong><code>render()</code></strong> should be called. By default, this method always returns <strong><code>true</code></strong>. But it can be overloaded to implement custom logic. It is the actual way to define custom render behavior in each React component.</p>\n<p>The <strong><code>shouldComponentUpdate()</code></strong> provides you with nextProp and nextState as arguments, which allows you to compare the current state and props of the component. For example, this code block will invoke <strong><code>render()</code></strong> only when the <strong><code>text</code></strong> prop changes:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">shouldComponentUpdate</span><span class=\"mtk1\">(</span><span class=\"mtk12\">nextProps</span><span class=\"mtk1\">: </span><span class=\"mtk12\">NewComponentProps</span><span class=\"mtk1\">, </span><span class=\"mtk12\">nextState</span><span class=\"mtk1\">: </span><span class=\"mtk12\">NewComponentState</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">this</span><span class=\"mtk1\">.</span><span class=\"mtk12\">props</span><span class=\"mtk1\">.</span><span class=\"mtk12\">text</span><span class=\"mtk1\"> !== </span><span class=\"mtk12\">nextProps</span><span class=\"mtk1\">.</span><span class=\"mtk12\">text</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">true</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    } </span><span class=\"mtk15\">else</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">false</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  }</span></span></code></pre>\n<p>The characteristics and behaviors mentioned above made it imperative that <strong><code>render()</code></strong> is a pure function. That means inside <strong><code>render()</code></strong>, you should not make an update to the component's states or props (no setState() call nor Redux state update). This makes sense because an update to the component will then trigger a new <strong><code>render()</code></strong> call, which can potentially lock you into an infinite render loop.</p>\n<p>In terms of return value: <strong><code>render()</code></strong> returns a single JSX element, as mentioned above. This comes with certain implications:</p>\n<ul>\n<li>If you need to return a collection of sibling elements, you need to wrap them all in a parent <strong><code>&#x3C;div></code></strong>, or a <strong><code>&#x3C;React.Fragment></code></strong>. It is worth noting that once rendered, <strong><code>&#x3C;React.Fragment></code></strong> will vanish from the DOM structure. It is only meant to be a wrapper component and does not appear in the final DOM in the browser. This makes it a more sensible choice over a <strong><code>&#x3C;div></code></strong> wrapping to avoid nesting div’s.</li>\n<li>JSX is immutable. The returned JSX element is a constant that represents the state of the DOM to be rendered. Therefore, when thinking about how to write a <strong><code>render()</code></strong> method, it is helpful to think about how the entire UI will look like at a moment in time, instead of thinking about how a certain element updates over time. <a href=\"https://reactjs.org/docs/rendering-elements.html#react-only-updates-whats-necessary\">“Thinking about how the UI should look at any given moment, rather than how to change it over time, eliminates a whole class of bugs.”</a></li>\n</ul>\n<h3 id=\"2-notes-on-reconciliation-and-the-strongcodekeycodestrong-prop\" style=\"position:relative;\"><a href=\"#2-notes-on-reconciliation-and-the-strongcodekeycodestrong-prop\" aria-label=\"2 notes on reconciliation and the strongcodekeycodestrong prop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Notes on reconciliation, and the <strong><code>key</code></strong> prop</h3>\n<p>The <strong><code>render()</code></strong> method in each React component then feeds into what is called the Reconciliation Algorithm. This is the primary algorithm that dictates how React renders the real DOM in your browser based on a <strong>virtual DOM </strong>maintained internally by React. To intelligently determine what needs to be rendered on every call, React compares the current state of the virtual DOM and the real one and only makes changes to the physical DOM where it recognizes that the UI has been updated.</p>\n<p>While not every single detail is known about React’s reconciliation algorithm, the characteristics detailed in the <a href=\"https://reactjs.org/docs/reconciliation.html\">official documentation</a> are enough for us to start phasing out certain suboptimal rendering patterns, thus writing a more robust <strong><code>render()</code></strong> method.</p>\n<ul>\n<li>On a UI update: As the DOM tree is parsed top to bottom, if a mismatch of elements is detected, React will <a href=\"https://reactjs.org/docs/reconciliation.html#elements-of-different-types\">tear down and rebuild the entire subtree</a> starting from that element. If the subtree is complex, this operation can be quite costly. Therefore, if a new element were to be introduced to the DOM tree, it should be appended as the last element in that level if there are no specific requirements of where it should be placed.</li>\n</ul>\n<p>For instance, given this DOM subtree:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-1&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-2&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-3&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>If a <strong><code>&#x3C;NewComponent></code></strong> is then added to the top of the list:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - element is detached and </span><span class=\"mtk17\">&lt;</span><span class=\"mtk10\">NewComponent</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span><span class=\"mtk1\"> instantiated --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk10\">NewComponent</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - content will be updated to &quot;list item 1&quot;  --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - content will be updated to &quot;list item 2&quot;  --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- new </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> is element created  --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>If instead <strong><code>&#x3C;NewComponent></code></strong> is added to the bottom: </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - no change --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - no change --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- previously </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> - no change --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- new instance of </span><span class=\"mtk17\">&lt;</span><span class=\"mtk10\">NewComponent</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span><span class=\"mtk1\"> is added --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk10\">NewComponent</span><span class=\"mtk1\"> </span><span class=\"mtk17\">/&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p> In the example above, appending <strong><code>&#x3C;NewComponent></code></strong> to the end of the list will result in 1 new instantiation, versus 1 instantiation, 3 updates and 1 tear down. In a larger application scale, this will prove to be a significant performance difference in the long run.</p>\n<ul>\n<li>\n<p>If you have ever tried to use the <strong><code>map()</code></strong> method to iterate over an array to render a list of elements, it is likely that you have seen React complaining about a missing <strong><code>key</code></strong> prop to each rendered list item. So what does the <strong><code>key</code></strong> actually do?</p>\n<p>A <strong><code>key</code></strong> is React’s way to recognize elements in the DOM tree, comes reconciliation time. When React is parsing all children of an element, it can leverage keys to match elements that were present from the last update. That allows you to shuffle the order of child elements without interfering with the algorithm. As long as the key matches between updates, React will preserve the element configuration.</p>\n</li>\n</ul>\n<p>Coming back to the example above, let’s add keys to all the existing list items:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-1&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-2&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-3&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>In this case, if we were to add an extra “list item 4” at the top of the list, it would not come with the same performance penalty:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"html\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- new item - </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\"> is instantiated --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-4&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 4</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- matched with the old &quot;li-1&quot; key - element stays unchanged between renders --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-1&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 1</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- matched with the old &quot;li-2&quot; key - element stays unchanged between renders --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-2&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 2</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  &lt;!-- matched with the old &quot;li-3&quot; key - element stays unchanged between renders --&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">span</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;li-3&quot;</span><span class=\"mtk17\">&gt;</span><span class=\"mtk1\">list item 3</span><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">span</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>In case a subtree is generated using a <strong><code>map()</code></strong> or other iterative methods, React requires that keys are provided with the element. However, even in case a DOM subtree is manually added, keys should be provided on subtrees that have complex behaviors regarding conditional rendering.</p>\n<h3 id=\"3-parting-words\" style=\"position:relative;\"><a href=\"#3-parting-words\" aria-label=\"3 parting words permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Parting words</h3>\n<p>React is a clever framework that offers performance through its rendering scheme, so it follows that, as developers, we should leverage it appropriately to help build performant applications. With that said, it is not a miracle device that optimizes out inefficiencies from the developer’s end, but a tool to be utilized. Understanding the <strong><code>render()</code></strong> nd its implication to the reconciliation algorithm is the first step to make sure we are leveraging the framework instead of working against it. It is also one of the first steps to mastering React.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk17 { color: #808080; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n</style>","headings":[{"value":"1. <strong><code>render()</code></strong> 101","depth":3},{"value":"2. Notes on reconciliation, and the <strong><code>key</code></strong> prop","depth":3},{"value":"3. Parting words","depth":3}],"fields":{"slug":"/engineering/understanding-react-rendering/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"Optimized rendering in the frontend is a crucial procedure. Let's learn how to optimize React rendering process.","title":"How to Render React with optimization","canonical":null,"date":"September 23, 2020","updated_date":null,"tags":["Node.js","React"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/56b6489418f8549fe50d61df309dd152/2ad7f/index.webp","srcSet":"/static/56b6489418f8549fe50d61df309dd152/1c9b5/index.webp 200w,\n/static/56b6489418f8549fe50d61df309dd152/f1752/index.webp 400w,\n/static/56b6489418f8549fe50d61df309dd152/2ad7f/index.webp 800w,\n/static/56b6489418f8549fe50d61df309dd152/e7405/index.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Nathan Nguyen","github":"nathannguyenn","bio":"Nathan Nguyen is a Software Engineer at LoginRadius. With deep interest in creating user-facing applications, he spends a lot of his time with web and mobile technology stacks, off and on the job, looking to build applications that are ever more intuitive and helpful to end users. Loves food, travel, photography and video games.","avatar":null}}}},"pageContext":{"id":"f135d4b5-42c2-5f4f-b0fc-9b224170ec28","fields__slug":"/engineering/understanding-react-rendering/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}