<?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: Christian Lisangola</title>
    <description>The latest articles on DEV Community by Christian Lisangola (@jochri3).</description>
    <link>https://dev.to/jochri3</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%2F354698%2F3f270fbf-a261-4adc-95ac-2673cf3f0ee8.jpeg</url>
      <title>DEV Community: Christian Lisangola</title>
      <link>https://dev.to/jochri3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jochri3"/>
    <language>en</language>
    <item>
      <title>Virtual DOM exposed</title>
      <dc:creator>Christian Lisangola</dc:creator>
      <pubDate>Wed, 31 Mar 2021 21:01:01 +0000</pubDate>
      <link>https://dev.to/jochri3/virtual-dom-exposed-3a39</link>
      <guid>https://dev.to/jochri3/virtual-dom-exposed-3a39</guid>
      <description>&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%2F9auloc5jtfatiyvuey1b.jpeg" 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%2F9auloc5jtfatiyvuey1b.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
For a while, I was wondering why several articles and tutorials about React say that the virtual DOM makes web applications made with &lt;strong&gt;React.js&lt;/strong&gt; perform better because of the way &lt;strong&gt;React.js&lt;/strong&gt; updates the &lt;strong&gt;DOM&lt;/strong&gt; or &lt;strong&gt;real DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here are the questions that made me look into the deeper workings of the &lt;strong&gt;virtual DOM&lt;/strong&gt; :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When working with the DOM directly, does a change to a single element or node trigger a refresh of the entire DOM? &lt;/li&gt;
&lt;li&gt;Are browsers that stupid to render the whole DOM tree just because there is a change on an element?&lt;/li&gt;
&lt;li&gt;If I were to create a browser, knowing the costly operation of rendering which includes painting, layout calculation, etc., does it make sense for me to let all this costly process repeat every time there is a change?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The purpose of this article is not to deny the importance of the &lt;strong&gt;virtual DOM&lt;/strong&gt;, but rather to &lt;em&gt;expose what it is not&lt;/em&gt; as it is presented here and there in several articles and even tutorials, and to give my point of view on the issue as I have experienced and understood the inner working and role of the &lt;strong&gt;virtual DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before going any further, let's talk about the DOM itself.&lt;/p&gt;

&lt;h2&gt;DOM&lt;/h2&gt;

&lt;p&gt;When you write your HTML, in a &lt;code&gt;.html&lt;/code&gt; extension file it is so that ultimately it can be read by a web browser in order to display its contents.&lt;br&gt;
The &lt;strong&gt;HTML&lt;strong&gt; code you write represents the &lt;strong&gt;&lt;em&gt;blueprint&lt;/em&gt;&lt;strong&gt; from which your browser will build the &lt;strong&gt;DOM&lt;/strong&gt; which stands for &lt;strong&gt;Document Object Model.&lt;/strong&gt;.&lt;/strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;Why Document? Why Object? Why Model?&lt;/h3&gt;

&lt;p&gt;Your &lt;strong&gt;HTML Document&lt;/strong&gt; is the &lt;strong&gt;Model&lt;/strong&gt; from which the browser will create the tree structure of your page so that &lt;code&gt;Javascript&lt;/code&gt; has a representation in the form of an &lt;code&gt;object&lt;/code&gt; that it will know how to manipulate.So, you know what &lt;strong&gt;DOM&lt;/strong&gt; means.&lt;/p&gt;

&lt;p&gt;Let's say your HTML code is as follows:&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;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Learn code&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Programming Languages&amp;lt;/h1&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li&amp;gt;Java&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;Go&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;Typescript&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;


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

&lt;/div&gt;



&lt;h4&gt;DOM tree for the above HTML Code&lt;/h4&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%2Fres.cloudinary.com%2Fjochri3%2Fimage%2Fupload%2Fv1617209452%2Fdev.to%2FDOM.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%2Fres.cloudinary.com%2Fjochri3%2Fimage%2Fupload%2Fv1617209452%2Fdev.to%2FDOM.png" title="DOM" alt="DOM!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the DOM tree is built, you can use a Javascript API to access these elements to modify their content, delete them, create new elements, etc.&lt;/p&gt;

&lt;p&gt;As Javascript has an object representation, there are methods for manipulation such as &lt;code&gt;document.getElementsByTagName('li')&lt;/code&gt; which returns a collection of li's or a &lt;code&gt;document.createElement('p')&lt;/code&gt; to create a paragraph element. Then these created elements can be added to the &lt;strong&gt;DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;Now let's go back to the virtual DOM of React.js&lt;/h2&gt;

&lt;h3&gt;What is really the &lt;strong&gt;virtual DOM&lt;/strong&gt;?&lt;/h3&gt;

&lt;p&gt;Before we talk about the &lt;strong&gt;Virtual DOM&lt;/strong&gt;, we need to think about how &lt;strong&gt;React.js&lt;/strong&gt; works and especially its &lt;strong&gt;&lt;em&gt;rendering cycle&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
In React.js user interfaces are broken down into reusable pieces of code usually written in &lt;strong&gt;JSX(Javascript Syntax Extension)&lt;/strong&gt;language.&lt;/p&gt;

&lt;p&gt;If for example we want to reuse a &lt;code&gt;button&lt;/code&gt; in several places in our code, we can create a &lt;code&gt;button&lt;/code&gt; component which is completely independent and which can be grafted in other components.&lt;/p&gt;

&lt;p&gt;In React.js a component can have &lt;code&gt;data&lt;/code&gt; that represents its &lt;code&gt;state&lt;/code&gt;, and when that &lt;code&gt;state&lt;/code&gt; is &lt;code&gt;updated&lt;/code&gt;, the component must &lt;strong&gt;rerender&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RERENDER!!!!&lt;/strong&gt;&lt;br&gt;
Here is the origin of all the confusion and misunderstanding of the Virtual DOM.&lt;/p&gt;

&lt;p&gt;Imagine that we have a rather complex component that represents an important block of your web page, and its &lt;code&gt;state&lt;/code&gt; is updated.Then imagine again that if it's just a very small part that changes but according to React's philosophy, the whole component should &lt;code&gt;rerender&lt;/code&gt;.If this change should directly be applied on the real DOM that will include includes repainting, layout calculation, etc..It will coast too much in terms of performance.&lt;/p&gt;

&lt;p&gt;So React developers had to find a solution to avoid unnecessary DOM updates, and this solution had to help React minimize the performance cost of such an operation.&lt;/p&gt;

&lt;p&gt;In the manipulation of the real &lt;strong&gt;DOM&lt;/strong&gt;, there is no complete &lt;code&gt;rerender&lt;/code&gt; when a part has changed because it is possible to target specific elements of the &lt;strong&gt;DOM&lt;/strong&gt; and apply changes to them that will not impact the whole &lt;code&gt;document&lt;/code&gt;, so to say that with the real DOM there is a rendering of the whole &lt;code&gt;DOM&lt;/code&gt; every time there is a change is completely false and unfounded. &lt;br&gt;
If &lt;strong&gt;React&lt;/strong&gt; solves the problem in terms of the performance, it's because of it own &lt;code&gt;rendering cycle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The way &lt;strong&gt;React.js&lt;/strong&gt; uses the &lt;strong&gt;virtual DOM&lt;/strong&gt; to solve its own problem is that every time it makes sure it has a copy of the &lt;strong&gt;DOM&lt;/strong&gt; in the form of a &lt;code&gt;Javascript object&lt;/code&gt;, and when the state updates, &lt;strong&gt;React&lt;/strong&gt; creates another version of the &lt;strong&gt;virtual DOM&lt;/strong&gt; that is another Javascript object that is a newer version with the updated state. Then it will apply a diffing algorithm to detect which parts have changed and then apply its changes only where needed in the &lt;strong&gt;DOM&lt;/strong&gt;, so that at some point React also ends up doing the expensive operation by &lt;strong&gt;&lt;em&gt;touching&lt;/em&gt;&lt;/strong&gt; the DOM even if it is more efficient because the whole DOM is not refreshed.&lt;/p&gt;

&lt;p&gt;In my opinion, one of the biggest advantages of Virtual DOM is the developer experience, because thanks to this philosophy, React offers us a &lt;strong&gt;&lt;em&gt;declarative API&lt;/em&gt;&lt;/strong&gt; that saves us from writing code to do the &lt;strong&gt;DOM&lt;/strong&gt; manipulation in an &lt;strong&gt;&lt;em&gt;imperative&lt;/em&gt;&lt;/strong&gt; way. From a development perspective it is a huge advantage and time saver to write &lt;strong&gt;&lt;em&gt;declarative&lt;/em&gt;&lt;/strong&gt; code with &lt;strong&gt;JSX&lt;/strong&gt; than code written with Vanilla Javascript or libraries like &lt;strong&gt;Jquery&lt;/strong&gt;. This code written in &lt;strong&gt;JSX&lt;/strong&gt; will then be translated into Javascript code that allows for the creation of &lt;strong&gt;React.js&lt;/strong&gt; component instances.&lt;/p&gt;

&lt;p&gt;If you have other questions, a contrary opinion or additional information do not hesitate to put them in the comments.&lt;/p&gt;

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