<?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: Fewwy</title>
    <description>The latest articles on DEV Community by Fewwy (@fewwy).</description>
    <link>https://dev.to/fewwy</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%2F439108%2Fdb7abc3b-132a-4e77-856e-81b2bb3a8ea0.jpg</url>
      <title>DEV Community: Fewwy</title>
      <link>https://dev.to/fewwy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fewwy"/>
    <language>en</language>
    <item>
      <title>React core concept - Virtual DOM explained</title>
      <dc:creator>Fewwy</dc:creator>
      <pubDate>Mon, 25 Jul 2022 13:45:53 +0000</pubDate>
      <link>https://dev.to/fewwy/react-core-concept-virtual-dom-explained-358m</link>
      <guid>https://dev.to/fewwy/react-core-concept-virtual-dom-explained-358m</guid>
      <description>&lt;p&gt;This article is part of a series that I'm writing to help people new to web development understand &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; better.&lt;br&gt;
When we use HTML + CSS + vanilla JavaScript, we often have to apply &lt;a href="https://en.wikipedia.org/wiki/Document_Object_Model"&gt;DOM&lt;/a&gt; methods that directly modify it. How do these methods work?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document.getElementByID('id').innerValue = 'new value'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this code the browser will go through these steps&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the element we want;&lt;/li&gt;
&lt;li&gt;Remove its child element;&lt;/li&gt;
&lt;li&gt;Update the element and add a 'new value' to it;&lt;/li&gt;
&lt;li&gt;Recalculate CSS for parent and child;&lt;/li&gt;
&lt;li&gt;Update layout;&lt;/li&gt;
&lt;li&gt;Display the updated element in the browser (rendering).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These six steps affect performance and make the code prone to mishaps.&lt;br&gt;
This fragility is one reason why libraries and frameworks like React, Angular, Vue, Svelte, Remix, and many others exist. &lt;/p&gt;

&lt;p&gt;React has gained popularity for several reasons, and one of them is how React solves the fragile code problem. All changes to the &lt;a href="https://en.wikipedia.org/wiki/Document_Object_Model"&gt;Document Object Model &lt;/a&gt;(which is a structured representation of HTML elements present on a web page) are first written to the Virtual DOM and then synchronized with the real DOM. This process is called &lt;a href="https://reactjs.org/docs/reconciliation.html"&gt;reconciliation&lt;/a&gt;. DOM changes are handled much faster because the browser doesn't have to render these changes – when we make changes to the React application code, they go into Virtual DOM, and only after that are they pushed to the real DOM.&lt;/p&gt;

&lt;p&gt;Let's try to understand this in more detail. When we change React application, we create a Virtual DOM, a tree of elements in the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EyqP5DkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kee5i8ggiab6b33gpyg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EyqP5DkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kee5i8ggiab6b33gpyg8.png" alt="Image description" width="880" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, this application has five elements – main page, container element for content, and menu with buttons. &lt;/p&gt;

&lt;p&gt;Let's say we made a request to the server and got a response with the new content – the changes will be written to the Virtual DOM, which will be compared to the real DOM and find a way to render it in a way that results in rendering the least number of elements. &lt;br&gt;
In this case, only the content will be rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N-nGdJ41--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4iem6wcea1og5txy2np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N-nGdJ41--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4iem6wcea1og5txy2np.png" alt="Image description" width="880" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say we made a request to the server and got a response with the new content – the changes will be written to the Virtual DOM, which will be compared to the real DOM and find a way to render it in a way that results in rendering the least number of elements. In this case, only the content will be rendered.&lt;/p&gt;

&lt;p&gt;How can this be faster than writing changes directly to the DOM?&lt;/p&gt;

&lt;p&gt;The point is that React keeps two copies of the Virtual DOM. One of them is a copy of the DOM (i.e., it doesn't contain our changes yet), and the second includes the changes we made. React can quickly compare the differences between the two Virtual DOMs (this process is called diffing). After that, React changes only the necessary parts of the real DOM. To do this, React uses a batch update. It means that all changes to the DOM happen all at once, not just one by one. The batch update improves the performance of the browser and the speed at which changes are rendered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;Let's summarize. One of React's core concepts is Virtual DOM, which improves the process of making changes to the browser's real DOM. It improves performance and increases the reliability of the application. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Making changes to the real DOM is insecure and slow (in terms of performance).&lt;/li&gt;
&lt;li&gt;The virtual DOM is a copy of the real DOM.&lt;/li&gt;
&lt;li&gt;The virtual DOM monitors application state changes.&lt;/li&gt;
&lt;li&gt;React uses an efficient diff algorithm to compare virtual DOM versions against each other.&lt;/li&gt;
&lt;li&gt;React uses batch update to update the real DOM.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/faq-internals.html"&gt;https://reactjs.org/docs/faq-internals.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/reactjs-virtual-dom/"&gt;https://www.geeksforgeeks.org/reactjs-virtual-dom/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://indepth.dev/posts/1501/exploring-how-virtual-dom-is-implemented-in-react"&gt;https://indepth.dev/posts/1501/exploring-how-virtual-dom-is-implemented-in-react&lt;/a&gt;&lt;/p&gt;

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