<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[React]]></title><description><![CDATA[React]]></description><link>https://nitishjha.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 14:10:14 GMT</lastBuildDate><atom:link href="https://nitishjha.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Rendering in React]]></title><description><![CDATA[What is rendering in react ?
React rendering is the process of converting react elements into UI elements that can be displayed on screen .
The react elements are simple javascript object which contains the structure which we want to display / render...]]></description><link>https://nitishjha.hashnode.dev/rendering-in-react</link><guid isPermaLink="true">https://nitishjha.hashnode.dev/rendering-in-react</guid><category><![CDATA[React]]></category><category><![CDATA[React rendering]]></category><category><![CDATA[virtual dom]]></category><category><![CDATA[React Reconciliation]]></category><category><![CDATA[Virtual DOM and Browser DOM in React.js]]></category><category><![CDATA[React elements]]></category><dc:creator><![CDATA[Nitish Jha]]></dc:creator><pubDate>Sun, 15 Dec 2024 09:09:38 GMT</pubDate><content:encoded><![CDATA[<p>What is rendering in react ?</p>
<p>React rendering is the process of converting react elements into UI elements that can be displayed on screen .</p>
<p>The react elements are simple javascript object which contains the structure which we want to display / render.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> element = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, React!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> reactElement = {
  <span class="hljs-attr">$$typeof</span>: <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'react.element'</span>),
  <span class="hljs-attr">type</span>: <span class="hljs-string">'div'</span>,
  <span class="hljs-attr">key</span>: <span class="hljs-literal">null</span>,
  <span class="hljs-attr">ref</span>: <span class="hljs-literal">null</span>,
  <span class="hljs-attr">props</span>: {
    <span class="hljs-attr">children</span>: <span class="hljs-string">'Hello, React!'</span>
  },
  <span class="hljs-attr">_owner</span>: <span class="hljs-literal">null</span>,
  <span class="hljs-attr">_store</span>: {}
}
</code></pre>
<ul>
<li><h3 id="heading-key-details">Key Details</h3>
<ul>
<li><p><code>$$typeof</code>: Identifies it as a React element</p>
</li>
<li><p><code>key</code>: Used for list rendering</p>
</li>
<li><p><code>ref</code>: Reference to a DOM element or component</p>
</li>
<li><p><code>_owner</code>: Internal React property tracking component hierarchy</p>
</li>
<li><p><code>_store</code>: Internal optimization object</p>
</li>
</ul>
</li>
</ul>
<p>These JavaScript objects are obtained from react elements using <code>React.createElement()</code> function calls by transpilers like Babel, therefore this abstraction layer provides a declarative way to describe UI and separates description and rendering logic unlike direct dom manipulation.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Equivalent React.createElement() call</span>
<span class="hljs-keyword">const</span> element = React.createElement(<span class="hljs-string">'h1'</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">'Hello, React!'</span>);
</code></pre>
<p>React elements are immutable objects and each update creates new react element (js object tree using diffing algo) for easy comparison.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Initial element</span>
<span class="hljs-keyword">let</span> counter = {
  <span class="hljs-attr">type</span>: <span class="hljs-string">'div'</span>,
  <span class="hljs-attr">props</span>: {
    <span class="hljs-attr">children</span>: <span class="hljs-string">'Count: 0'</span>
  }
};

<span class="hljs-comment">// New element (creating a new object, not modifying)</span>
counter = {
  <span class="hljs-attr">type</span>: <span class="hljs-string">'div'</span>,
  <span class="hljs-attr">props</span>: {
    <span class="hljs-attr">children</span>: <span class="hljs-string">'Count: 1'</span>
  }
};
</code></pre>
<p>Whenever any event triggers, react collects all the state updates, batches together and creates new virtual DOM Tree, unlike immediate rendering by DOM.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inefficient </span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateUI</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Each update triggers immediate DOM manipulation</span>
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'count'</span>).textContent = newCount;
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'status'</span>).className = newStatus;
}
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);
  <span class="hljs-keyword">const</span> [clicked, setClicked] = useState(<span class="hljs-literal">false</span>);

  <span class="hljs-keyword">const</span> handleClick = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Multiple state updates in a single event handler</span>
    setCount(<span class="hljs-function"><span class="hljs-params">c</span> =&gt;</span> c + <span class="hljs-number">1</span>);   <span class="hljs-comment">// Update 1</span>
    setClicked(<span class="hljs-literal">true</span>);       <span class="hljs-comment">// Update 2</span>
    <span class="hljs-comment">// React will batch these updates together</span>
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleClick}</span>&gt;</span>Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Therefore, react Elements are more efficient because there is no immediate rendering, it collects multiple state updates on an event and process together instead multiple immediate DOM manipulation for the same event and it only updates what has changed by comparing previous virtual DOM tree with new Virtual DOM, it's able to do because react elements are immutable objects and each update creates new react element</p>
<p>Therefore when state changes new virtual Dom tree ,compares with the previous virtual dom tree, calculate minimal change and efficiently updates real Dom. This is called <em>react's reconciliation process</em>.</p>
<p>Virtual DOM is a Tree-like structure of React elements. It's essentially a JavaScript object that mirrors the structure of the real DOM, but is much faster to manipulate.</p>
<h2 id="heading-html-vs-dom-vs-virtual-dom-object">HTML vs DOM vs Virtual DOM Object</h2>
<p>HTML Representation</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//css</span>
 &lt;style&gt;
        #user-profile {
            background-color: blue;
            color: white;
            padding: <span class="hljs-number">10</span>px;
        }
    &lt;/style&gt;

<span class="hljs-comment">//   html</span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> 
        <span class="hljs-attr">id</span>=<span class="hljs-string">"user-profile"</span> 
        <span class="hljs-attr">class</span>=<span class="hljs-string">"profile active"</span> 
        <span class="hljs-attr">data-user-id</span>=<span class="hljs-string">"123"</span>
    &gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>John Doe<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>

<span class="hljs-comment">//Javscript</span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">

        <span class="hljs-keyword">const</span> divElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'user-profile'</span>);
        divElement.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">() =&gt;</span> {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Div clicked'</span>);
        });

        <span class="hljs-built_in">console</span>.log(divElement.offsetWidth);  <span class="hljs-comment">// Rendered width</span>
        <span class="hljs-built_in">console</span>.log(divElement.clientHeight); <span class="hljs-comment">// Inner height</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<p>Abstract Representation of a DOM element object contains basically these 5 properties -</p>
<pre><code class="lang-javascript">{
  <span class="hljs-attr">nodeType</span>: <span class="hljs-number">1</span>,            <span class="hljs-comment">// Always exists for elements</span>
  <span class="hljs-attr">tagName</span>: <span class="hljs-string">'ELEMENT_TAG'</span>, <span class="hljs-comment">// Tag name of the element (e.g., 'DIV', 'H1')</span>
  <span class="hljs-attr">attributes</span>: NamedNodeMap {}, <span class="hljs-comment">// Attribute collection (empty if no attributes)</span>
  <span class="hljs-attr">childNodes</span>: NodeList [],     <span class="hljs-comment">// List of children (empty if no children)</span>
  <span class="hljs-attr">parentNode</span>: Element || <span class="hljs-literal">null</span>  <span class="hljs-comment">// Parent node (null for &lt;html&gt;)</span>
}
</code></pre>
<pre><code class="lang-javascript">{

  <span class="hljs-attr">nodeType</span>: <span class="hljs-number">1</span>,  <span class="hljs-comment">// Element node type</span>
  <span class="hljs-attr">tagName</span>: <span class="hljs-string">'DIV'</span>,
  <span class="hljs-attr">nodeName</span>: <span class="hljs-string">'DIV'</span>,

  <span class="hljs-comment">// Identification Properties</span>
  <span class="hljs-attr">id</span>: <span class="hljs-string">'user-profile'</span>,
  <span class="hljs-attr">className</span>: <span class="hljs-string">'profile active'</span>,

  <span class="hljs-comment">// Attributes Collection</span>
  <span class="hljs-attr">attributes</span>: NamedNodeMap {
    <span class="hljs-string">'id'</span>: {
      <span class="hljs-attr">name</span>: <span class="hljs-string">'id'</span>,
      <span class="hljs-attr">value</span>: <span class="hljs-string">'user-profile'</span>
    },
    <span class="hljs-string">'class'</span>: {
      <span class="hljs-attr">name</span>: <span class="hljs-string">'class'</span>,
      <span class="hljs-attr">value</span>: <span class="hljs-string">'profile active'</span>
    },
    <span class="hljs-string">'data-user-id'</span>: {
      <span class="hljs-attr">name</span>: <span class="hljs-string">'data-user-id'</span>,
      <span class="hljs-attr">value</span>: <span class="hljs-string">'123'</span>
    }
  },

  <span class="hljs-comment">// Custom Data Attributes</span>
  <span class="hljs-attr">dataset</span>: {
    <span class="hljs-attr">userId</span>: <span class="hljs-string">'123'</span>
  },

  <span class="hljs-comment">// Style Properties</span>
  <span class="hljs-attr">style</span>: CSSStyleDeclaration {
    <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'blue'</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
    <span class="hljs-attr">padding</span>: <span class="hljs-string">'10px'</span>,
    <span class="hljs-comment">// Many other inherited CSS properties</span>
  },

  <span class="hljs-comment">// Event Listeners</span>
  <span class="hljs-attr">_listeners</span>: {
    <span class="hljs-attr">click</span>: [
      {
        <span class="hljs-attr">type</span>: <span class="hljs-string">'click'</span>,
        <span class="hljs-attr">listener</span>: <span class="hljs-function">() =&gt;</span> {
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Div clicked'</span>)
        },
        <span class="hljs-attr">capture</span>: <span class="hljs-literal">false</span>
      }
    ]
  },

  <span class="hljs-comment">// Child Node Relationship</span>
  <span class="hljs-attr">childNodes</span>: [
    {
      <span class="hljs-attr">nodeType</span>: <span class="hljs-number">1</span>,
      <span class="hljs-attr">tagName</span>: <span class="hljs-string">'H2'</span>,
      <span class="hljs-attr">textContent</span>: <span class="hljs-string">'John Doe'</span>,
      <span class="hljs-comment">// Additional H2 element properties</span>
    }
  ],

  <span class="hljs-comment">// Parent-Child Methods</span>
  <span class="hljs-attr">appendChild</span>: [<span class="hljs-built_in">Function</span>],
  <span class="hljs-attr">removeChild</span>: [<span class="hljs-built_in">Function</span>],
  <span class="hljs-attr">insertBefore</span>: [<span class="hljs-built_in">Function</span>],

  <span class="hljs-comment">// Dimensional Properties</span>
  <span class="hljs-attr">offsetWidth</span>: <span class="hljs-number">0</span>,  <span class="hljs-comment">// Will be calculated when rendered</span>
  <span class="hljs-attr">offsetHeight</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">clientWidth</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">clientHeight</span>: <span class="hljs-number">0</span>,

  <span class="hljs-comment">// Positioning Properties</span>
  <span class="hljs-attr">offsetTop</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">offsetLeft</span>: <span class="hljs-number">0</span>,

  <span class="hljs-comment">// Traversal Methods</span>
  <span class="hljs-attr">querySelector</span>: [<span class="hljs-built_in">Function</span>],
  <span class="hljs-attr">querySelectorAll</span>: [<span class="hljs-built_in">Function</span>],

  <span class="hljs-comment">// Event Methods</span>
  <span class="hljs-attr">addEventListener</span>: [<span class="hljs-built_in">Function</span>],
  <span class="hljs-attr">removeEventListener</span>: [<span class="hljs-built_in">Function</span>],

  <span class="hljs-comment">// Inheritance</span>
  <span class="hljs-attr">__proto__</span>: HTMLDivElement.prototype
}
</code></pre>
<p>React Element JS Object</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// React equivalent (Lightweight)</span>
{
  <span class="hljs-attr">type</span>: <span class="hljs-string">'div'</span>,
  <span class="hljs-attr">props</span>: {
    <span class="hljs-attr">id</span>: <span class="hljs-string">'user-profile'</span>,
    <span class="hljs-attr">className</span>: <span class="hljs-string">'profile active'</span>,
    <span class="hljs-attr">style</span>: {
      <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'blue'</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
      <span class="hljs-attr">padding</span>: <span class="hljs-string">'10px'</span>
    },
    <span class="hljs-string">'data-user-id'</span>: <span class="hljs-string">'123'</span>,
    <span class="hljs-attr">children</span>: [
      {
        <span class="hljs-attr">type</span>: <span class="hljs-string">'h2'</span>,
        <span class="hljs-attr">props</span>: {
          <span class="hljs-attr">children</span>: <span class="hljs-string">'John Doe'</span>
        }
      }
    ]
  }
}
</code></pre>
<p>Hence,</p>
<ul>
<li><p>Abstraction layer which provides declarative way to describe UI and separates rendering logic using React.createElement()</p>
</li>
<li><p>the immutability of react element objects for comparision and optimization,</p>
</li>
<li><p>and light weight object representation makes React rendering more efficient.</p>
</li>
</ul>
<p>There are ways in which rendering can be made more efficient using Memoization, LazyLoading and code splitting. We will discuss this in next post.</p>
]]></content:encoded></item></channel></rss>