<?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: Amul Gaurav</title>
    <description>The latest articles on DEV Community by Amul Gaurav (@amulgaurav).</description>
    <link>https://dev.to/amulgaurav</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%2F1205762%2F5cd9884e-a59e-44e1-aa14-656bad7f36ab.jpeg</url>
      <title>DEV Community: Amul Gaurav</title>
      <link>https://dev.to/amulgaurav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amulgaurav"/>
    <language>en</language>
    <item>
      <title>Track how many times a component rendered using useRef() - React</title>
      <dc:creator>Amul Gaurav</dc:creator>
      <pubDate>Fri, 18 Oct 2024 11:51:27 +0000</pubDate>
      <link>https://dev.to/amulgaurav/track-how-many-times-a-component-rendered-using-useref-react-42j6</link>
      <guid>https://dev.to/amulgaurav/track-how-many-times-a-component-rendered-using-useref-react-42j6</guid>
      <description>&lt;p&gt;First let's think of all the ways how a beginner try to tackle this.&lt;/p&gt;

&lt;p&gt;To follow along if you want to, create a new &lt;code&gt;react&lt;/code&gt; project and put this much code in your &lt;code&gt;App.jsx&lt;/code&gt; or any component of your choice and also remove everything from &lt;code&gt;index.css&lt;/code&gt; and &lt;code&gt;App.css&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleRender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;rendered&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleRender&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Force&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It'll preview like this right now:&lt;br&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%2F7hn3njmtctby8po78byy.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%2F7hn3njmtctby8po78byy.png" alt="Image description" width="276" height="111"&gt;&lt;/a&gt;&lt;br&gt;
In the above code, I've create a state variable named &lt;code&gt;count&lt;/code&gt; which increments by &lt;code&gt;1&lt;/code&gt; on clicking the &lt;code&gt;button&lt;/code&gt;. The &lt;code&gt;count&lt;/code&gt; here is an extra state variable which doesn't doing anything here but it will help you understand things better. This code shouldn't overwhelm you if you've previously coded in React.&lt;/p&gt;

&lt;p&gt;So, let's get to the main part...&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Using State Variable
&lt;/h3&gt;

&lt;p&gt;This is the first approach which come to most of us minds like create a state variable initializing it with &lt;code&gt;0&lt;/code&gt; and increment it by &lt;code&gt;1&lt;/code&gt; every time component renders.&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;App&lt;/code&gt; component will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setNumberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleRender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setNumberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;rendered&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleRender&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Force&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach will work fine here but it has many flaws. The component ideally isn't rendering only once when we click &lt;code&gt;Force Render&lt;/code&gt; button. When we click the button the &lt;code&gt;count&lt;/code&gt; value increments by &lt;code&gt;1&lt;/code&gt; which causes the component to re-render as we all know change in any state variable causes a component to re-render. But it does end here after a re-render by &lt;code&gt;setCount(count + 1)&lt;/code&gt;, &lt;code&gt;setNumberOfTimesRendered(numberOfTimesRendered + 1)&lt;/code&gt; will be triggered and causes the component to render again.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;numberOfTimesRendered&lt;/code&gt; value increments by &lt;code&gt;1&lt;/code&gt; but every time we click the &lt;code&gt;Force Render&lt;/code&gt; button the component is rendering twice which shows why using the state variable for this kind of matter is not the &lt;code&gt;best&lt;/code&gt; but &lt;code&gt;worst&lt;/code&gt; approach here.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Using Global Variable
&lt;/h3&gt;

&lt;p&gt;Another approach one can think of is defining a global variable outside the component life-cycle and increment it by &lt;code&gt;1&lt;/code&gt; when component re-renders, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleRender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;rendered&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleRender&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Force&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach will work fine and it doesn't cause any extra renders and this seems to be the &lt;code&gt;best&lt;/code&gt; solution for this but it isn't as you shouldn't define anything outside the component life-cycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Using Local Variable
&lt;/h3&gt;

&lt;p&gt;One can also think of the same approach as above but with local variable inside the component but let me tell you, there isn't any &lt;code&gt;worst&lt;/code&gt; approach than this because every time the component render the local variable will re-initialize to &lt;code&gt;0&lt;/code&gt; and we'll lost track of the previous renders every time.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Using useRef() hook
&lt;/h3&gt;

&lt;p&gt;Now let's implement this using &lt;code&gt;useRef()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As most of us know &lt;code&gt;useRef&lt;/code&gt; is a hook to reference elements inside the dom and manipulate them manually but &lt;code&gt;useRef&lt;/code&gt; is not just limited to this. It can also hold values and it can be used to keep a variable persistent across component rendering life-cycle.&lt;/p&gt;

&lt;p&gt;If you store a value inside the &lt;code&gt;useRef&lt;/code&gt;, it will not be affected by the React component rendering life-cycle, the value will remain persistent across any amount of re-renders. The implementation will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleRender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;rendered&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;numberOfTimesRendered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleRender&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Force&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;numberOfTimesRendered.current&lt;/code&gt; gives the current value stored inside the &lt;code&gt;reference&lt;/code&gt; variable. This approach doesn't cause any extra re-renders not saying it is the &lt;code&gt;best&lt;/code&gt; but one of the most &lt;code&gt;optimal&lt;/code&gt; approach as of my knowledge.&lt;/p&gt;




&lt;p&gt;That concludes this article. Open for feedbacks and if you need further clarification, please comment below, and I'll do my best to assist you.&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="https://twitter.com/amultwts" rel="noopener noreferrer"&gt;X&lt;/a&gt; &amp;amp; &lt;a href="https://www.linkedin.com/in/amul-gaurav/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;!🔔✨&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Stack vs Heap memory</title>
      <dc:creator>Amul Gaurav</dc:creator>
      <pubDate>Sat, 27 Jul 2024 17:34:35 +0000</pubDate>
      <link>https://dev.to/amulgaurav/stack-vs-heap-memory-43d1</link>
      <guid>https://dev.to/amulgaurav/stack-vs-heap-memory-43d1</guid>
      <description>&lt;p&gt;This concept isn't different to any other programming language, so any &lt;em&gt;literature&lt;/em&gt; about heap and stack applies the same to your preferred programming language. So, you might not want to skip it thinking this probably doesn't apply to the language in which I code. Let's dive right in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Stack Memory
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Location&lt;/u&gt;:&lt;/strong&gt; Stack memory resides in RAM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Purpose&lt;/u&gt;:&lt;/strong&gt; It is used for static memory allocation, which includes storing function parameters, return addresses, local variables, and control flow data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Structure&lt;/u&gt;:&lt;/strong&gt; The stack operates in a Last In, First Out (&lt;em&gt;LIFO&lt;/em&gt;) manner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Size:&lt;/u&gt;&lt;/strong&gt; The size of the stack is generally determined at the start of a program and is limited. The exact size can vary depending on the operating system and the architecture, but it is typically much smaller than the heap. Practically, stack size might be a few &lt;em&gt;megabytes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Management:&lt;/u&gt;&lt;/strong&gt; Stack memory is managed automatically by the CPU, with memory being allocated and deallocated as functions are called and return.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Heap Memory
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Location:&lt;/u&gt;&lt;/strong&gt; Heap memory also resides in RAM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Purpose:&lt;/u&gt;&lt;/strong&gt; It is used for dynamic memory allocation, where memory blocks are allocated and freed as needed by the program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Structure:&lt;/u&gt;&lt;/strong&gt; The heap does not follow a strict order and can grow and shrink dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Size:&lt;/u&gt;&lt;/strong&gt; The heap size is generally much larger than the stack size. It can be many &lt;em&gt;gigabytes&lt;/em&gt;, depending on the system's available memory and configuration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Management:&lt;/u&gt;&lt;/strong&gt; Heap memory is managed manually by the programmer using allocation (&lt;em&gt;malloc&lt;/em&gt;, &lt;em&gt;new&lt;/em&gt;) and deallocation (&lt;em&gt;free&lt;/em&gt;, &lt;em&gt;delete&lt;/em&gt;) functions. Some modern languages (like &lt;strong&gt;Java&lt;/strong&gt; &amp;amp; &lt;strong&gt;Python&lt;/strong&gt;) have &lt;em&gt;garbage collectors&lt;/em&gt; to manage heap memory automatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's discuss when you write code how your variables get stored.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. 1st Scenario:
&lt;/h3&gt;

&lt;p&gt;If you want your variable to be available in the function scope &amp;amp; gets cleared from the memory as soon as the function returns, you will declare it like you normally do &amp;amp; the variable will get stored in Stack memory. A stack frame is added when a function is called and removed when the function returns. Here's an example how you can do that in JAVA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static void sample() {
    int a = 5;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code block, the variable &lt;code&gt;a&lt;/code&gt; is defined in the &lt;code&gt;sample&lt;/code&gt; function scope &amp;amp; will only be available in that scope when the &lt;code&gt;sample&lt;/code&gt; function gets called. It will be cleared from the &lt;em&gt;stack&lt;/em&gt; as soon as the &lt;code&gt;sample&lt;/code&gt; function call returns.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. 2nd Scenario:
&lt;/h3&gt;

&lt;p&gt;If you don't want the behavior like in &lt;strong&gt;Scenario 1&lt;/strong&gt;. There are 2 options to avoid that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using Stack:&lt;/strong&gt; as soon as the function call finishes and function gets cleared from the stack, the variable stored gets cleared from its current function call stack &amp;amp; gets copied to its parent function call stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using Heap:&lt;/strong&gt; The most preferred &amp;amp; efficient way to achieve this is to create a &lt;em&gt;pointer&lt;/em&gt;. A &lt;strong&gt;pointer&lt;/strong&gt; is a reference variable which is stored in stack memory points to a block of heap memory. Such an allocation is independent of function calls. This means the heap can become fragmented and the OS needs to keep track of which memory is used and which is free. This is quite a lot of work compared to the stack where only one pointer is used to point to the currently active frame. The data stored in heap memory can be pointed or referenced by more than one reference variable.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Student student = new Student();

// student -&amp;gt; object of class Student

// student -&amp;gt; reference variable

// new Student() -&amp;gt; new keyword dynamically allocates memory for student object of class Student() in heap memory

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

&lt;/div&gt;



&lt;p&gt;When there is no or zero reference variable pointing to the memory allocated in heap, it's get garbage collected by the Garbage Collector, in case of languages that have garbage collector like &lt;em&gt;JAVA&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In languages like &lt;em&gt;C&lt;/em&gt; or &lt;em&gt;C++&lt;/em&gt; where there is no concept of &lt;em&gt;Garbage Collectors&lt;/em&gt;, the memory allocated in heap must be handled manually by the programmer using (&lt;em&gt;malloc&lt;/em&gt;, &lt;em&gt;new&lt;/em&gt;) functions for allocation and (&lt;em&gt;free&lt;/em&gt;, &lt;em&gt;delete&lt;/em&gt;) functions for deallocation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9yltt6tce9elunkkpy7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9yltt6tce9elunkkpy7.png" alt="pointer memory allocation" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stack&lt;/strong&gt; is &lt;em&gt;simple&lt;/em&gt;, &lt;em&gt;fast&lt;/em&gt; and &lt;em&gt;limited&lt;/em&gt;. &lt;strong&gt;Heap&lt;/strong&gt; is &lt;em&gt;flexible&lt;/em&gt; and almost &lt;em&gt;unlimited&lt;/em&gt; but slow to allocate and a lot of work for the OS to manage.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt; When primitives are declared as &lt;em&gt;instance variables&lt;/em&gt; within a class, they are part of the object's memory and are allocated on the &lt;em&gt;heap&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://youtu.be/Vlzpk9sN3UU?si=hVjKIOc85RNrdUKB" rel="noopener noreferrer"&gt;Learning Go: A Discussion of the Language with the Author, Jon Bodner | 028&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/rust/comments/1b2tiod/trying_to_understand_stack_heap/" rel="noopener noreferrer"&gt;Trying to understand Stack &amp;amp; Heap&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That concludes this article. If you need further clarification, please comment below, and I'll do my best to assist you.&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="https://twitter.com/amultwts" rel="noopener noreferrer"&gt;X&lt;/a&gt; &amp;amp; &lt;a href="https://www.linkedin.com/in/amul-gaurav/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;!🔔✨&lt;/p&gt;

</description>
      <category>java</category>
      <category>coding</category>
      <category>programming</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
