<?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[Sudip Kundu]]></title><description><![CDATA[UI Engineer @Flipkart
I like to explore new technologies & build projects on them.]]></description><link>https://blog.sudipkundu.com</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 22:21:22 GMT</lastBuildDate><atom:link href="https://blog.sudipkundu.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Implement Infinite Scroll in Ant Design Table]]></title><description><![CDATA[If you’ve ever worked with Ant Design’s Table component, you probably love how powerful it is — sorting, pagination, filtering, you name it.
But one thing it doesn’t provide out of the box - Infinite scrolling!
The Problem
When you’re dealing with th...]]></description><link>https://blog.sudipkundu.com/how-to-implement-infinite-scroll-in-ant-design-table</link><guid isPermaLink="true">https://blog.sudipkundu.com/how-to-implement-infinite-scroll-in-ant-design-table</guid><category><![CDATA[infinite scroll with virtualization]]></category><category><![CDATA[antd table component]]></category><category><![CDATA[Ant Design]]></category><category><![CDATA[infinite scrolling]]></category><category><![CDATA[virtualization]]></category><category><![CDATA[antd]]></category><dc:creator><![CDATA[Sudip Kundu]]></dc:creator><pubDate>Sat, 30 Aug 2025 19:15:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756583329602/696f737e-fc21-4695-a24a-42595ed3d336.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you’ve ever worked with Ant Design’s Table component, you probably love how powerful it is — sorting, pagination, filtering, you name it.</p>
<p>But one thing it doesn’t provide out of the box - Infinite scrolling!</p>
<h2 id="heading-the-problem">The Problem</h2>
<p>When you’re dealing with thousands of rows, the usual approaches are:</p>
<ul>
<li><p><strong>Pagination</strong>: Works, but breaks the smooth “scrolling through data” experience.</p>
</li>
<li><p><strong>Load All Data</strong>: Possible for small datasets, but if you try with 1,000+ rows… the table will lag, the DOM will bloat, and your browser will cry.</p>
</li>
</ul>
<p>What we really need is a way to:</p>
<ol>
<li><p>Only render what’s visible (virtualization)</p>
</li>
<li><p>Fetch more rows as the user scrolls (infinite loading)</p>
</li>
</ol>
<p>Unfortunately, Ant Design only supports virtualization and doesn’t support infinite scrolling out of the box.</p>
<h2 id="heading-the-solution">The Solution</h2>
<p>I built a lightweight <a target="_blank" href="https://www.npmjs.com/package/react-virtual-infinite-scroll-hook"><code>useVirtualInfiniteScroll</code></a> hook that makes it super easy to add <strong>infinite scroll</strong> to scrollable list with virtual scrollbars.</p>
<h2 id="heading-example-code-with-ant-design-table">Example Code (with Ant Design Table)</h2>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { Table } <span class="hljs-keyword">from</span> <span class="hljs-string">"antd"</span>;
<span class="hljs-keyword">import</span> { fetchData } <span class="hljs-keyword">from</span> <span class="hljs-string">"../helpers/fetchData"</span>;
<span class="hljs-keyword">import</span> { COLUMNS } <span class="hljs-keyword">from</span> <span class="hljs-string">"../helpers/constants"</span>;
<span class="hljs-keyword">import</span> useVirtualInfiniteScroll <span class="hljs-keyword">from</span> <span class="hljs-string">"react-virtual-infinite-scroll-hook"</span>;

<span class="hljs-keyword">const</span> AntdTableVirtualWithInfiniteScroll: React.FC = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [data, setData] = useState&lt;<span class="hljs-built_in">any</span>[]&gt;([]);
  <span class="hljs-keyword">const</span> [page, setPage] = useState(<span class="hljs-number">1</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">false</span>);
  <span class="hljs-keyword">const</span> [hasMore, setHasMore] = useState(<span class="hljs-literal">true</span>);

  <span class="hljs-keyword">const</span> loadMore = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">if</span> (loading || !hasMore) <span class="hljs-keyword">return</span>;
    setLoading(<span class="hljs-literal">true</span>);
    <span class="hljs-keyword">const</span> newData = <span class="hljs-keyword">await</span> fetchData(page, <span class="hljs-number">10</span>);
    setData(<span class="hljs-function">(<span class="hljs-params">prev</span>) =&gt;</span> [...prev, ...newData]);
    setHasMore(newData.length &gt; <span class="hljs-number">0</span>);
    setPage(<span class="hljs-function">(<span class="hljs-params">p</span>) =&gt;</span> p + <span class="hljs-number">1</span>);
    setLoading(<span class="hljs-literal">false</span>);
  };

  <span class="hljs-comment">// https://www.npmjs.com/package/react-virtual-infinite-scroll-hook</span>
  <span class="hljs-comment">// Just import &amp; call the hook anywhere passing the required props</span>
  useVirtualInfiniteScroll({
    onLoadMore: loadMore,
    hasMore,
    loading,
    scrollbarSelector: <span class="hljs-string">".ant-table-tbody-virtual-scrollbar-vertical"</span>,
    enabled: <span class="hljs-literal">true</span>,
  });

  useEffect(<span class="hljs-function">() =&gt;</span> {
    loadMore();
  }, []);

  <span class="hljs-keyword">return</span> (
    &lt;div style={{ maxWidth: <span class="hljs-number">700</span> }}&gt;
      &lt;Table
        bordered
        columns={COLUMNS}
        loading={loading}
        dataSource={data}
        pagination={<span class="hljs-literal">false</span>} <span class="hljs-comment">//👈 disable pagination</span>
        virtual={<span class="hljs-literal">true</span>} <span class="hljs-comment">// 👈 enable virtual table</span>
        scroll={{ y: <span class="hljs-number">450</span> }} <span class="hljs-comment">// 👈 Virtual table container height</span>
      /&gt;
    &lt;/div&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> AntdTableVirtualWithInfiniteScroll;
</code></pre>
<h2 id="heading-why-this-works">Why This Works</h2>
<ul>
<li><p>The hook only passes the <strong>visible slice</strong> of data to the table.</p>
</li>
<li><p>As you scroll near the bottom, it automatically asks for more rows.</p>
</li>
<li><p>The DOM never gets overloaded, so scrolling stays smooth even with huge datasets.</p>
</li>
</ul>
<h2 id="heading-lets-check-out-usevirtualinfinitescrollhttpswwwnpmjscompackagereact-virtual-infinite-scroll-hook-hook-in-action">Let’s check out <a target="_blank" href="https://www.npmjs.com/package/react-virtual-infinite-scroll-hook"><code>useVirtualInfiniteScroll</code></a> hook in action</h2>
<iframe src="https://codesandbox.io/embed/qf2q8l?view=preview&amp;hidenavigation=1" style="width:100%;height:700px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>Ant Design Tables are fantastic for enterprise apps, but when it comes to <strong>scroll-heavy datasets</strong>, we need to go beyond pagination and use infinite scrolling with virtualization.</p>
<p>With <a target="_blank" href="https://www.npmjs.com/package/react-virtual-infinite-scroll-hook"><code>useVirtualInfiniteScroll</code></a> hook, you can supercharge your Ant Design Table with infinite scrolling and virtualization in just a few lines of code. The hook is available as a npm package - <a target="_blank" href="https://www.npmjs.com/package/react-virtual-infinite-scroll-hook?utm_source=blog.sudipkundu.com">https://www.npmjs.com/package/react-virtual-infinite-scroll-hook</a></p>
<p>Give it a try in your next project and let me know how it works for you! 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Event Propagation in JavaScript]]></title><description><![CDATA[Introduction
An event travels or propagates through the DOM tree, this is called Event propagation.
This can happen in 2 ways - 

From top to bottom
From bottom to top

Let's understand using an example

Take 3 divs, a grandparent div, inside that we...]]></description><link>https://blog.sudipkundu.com/event-propagation-in-javascript</link><guid isPermaLink="true">https://blog.sudipkundu.com/event-propagation-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[web performance]]></category><category><![CDATA[performance]]></category><dc:creator><![CDATA[Sudip Kundu]]></dc:creator><pubDate>Thu, 12 May 2022 14:21:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652301961931/tBpZb5f2G.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-introduction">Introduction</h1>
<p>An event travels or propagates through the DOM tree, this is called Event propagation.</p>
<p>This can happen in 2 ways - </p>
<ul>
<li>From top to bottom</li>
<li>From bottom to top</li>
</ul>
<h4 id="heading-lets-understand-using-an-example">Let's understand using an example</h4>
<ul>
<li>Take 3 divs, a grandparent div, inside that we have a parent div and inside that, we have our target div.</li>
</ul>
<pre><code><span class="hljs-operator">&lt;</span>div id<span class="hljs-operator">=</span><span class="hljs-string">"grand-parent"</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>div id<span class="hljs-operator">=</span><span class="hljs-string">"parent"</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span>div id<span class="hljs-operator">=</span><span class="hljs-string">"target"</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
</code></pre><ul>
<li>Attach onClick handlers to all divs console logging the name of the divs.</li>
</ul>
<pre><code>grandParent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Grand Parent"</span>))
parent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Parent"</span>))
target.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Target"</span>))
</code></pre><p>-Now click on the target div (smallest one)</p>
<iframe src="https://codesandbox.io/embed/event-bubbling-9j7rwi?expanddevtools=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<h3 id="heading-default-order-of-onclick-event">Default order of onClick event</h3>
<ul>
<li>We get <em>Target =&gt; Parent =&gt; Grandparent</em> logged in the console.</li>
<li>This is an example of the bottom to top event propagation or <em>Event Bubbling</em>.</li>
<li>So, without doing anything we got to see the event bubbling, so it is safe to say the onClick event by default bubbles up. (Not all events bubble up by default)</li>
</ul>
<blockquote>
<p>Remember event bubbling as bubbles coming up to the top surface of a water</p>
</blockquote>
<h3 id="heading-reversing-the-order-of-events">Reversing the order of events</h3>
<p>Now, we want to reverse the order of events. But how do we do that? There's a third parameter taken by the addEventListner which accepts a boolean value, which is false by default. This attribute tells the handler if the event is to be captured or not.</p>
<p>If the third parameter is set to true the event capturing mode is enabled.</p>
<pre><code>grandParent.addEventListener(<span class="hljs-string">"click"</span>,()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>console.log(<span class="hljs-string">"Grand Parent"</span>),<span class="hljs-literal">true</span>)
parent.addEventListener(<span class="hljs-string">"click"</span>,()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>console.log(<span class="hljs-string">"Parent"</span>),<span class="hljs-literal">true</span>)
target.addEventListener(<span class="hljs-string">"click"</span>,()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>console.log(<span class="hljs-string">"Target"</span>),<span class="hljs-literal">true</span>)
</code></pre><p>-Now click on the target div (smallest one)</p>
<iframe src="https://codesandbox.io/embed/event-capturing-esi71z?expanddevtools=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<ul>
<li>We get Grandparent  =&gt; Parent =&gt; Target logged in the console.</li>
<li>This is an example of top to bottom event propagation or <em>Event Capturing/Trickling</em>.</li>
</ul>
<blockquote>
<p>Remember event capturing/trickling as water trickling down the slope of a mountain</p>
</blockquote>
<h2 id="heading-order-of-event-propagation">Order of event propagation</h2>
<p>Consider the code below where only the parent event has capture set to true</p>
<pre><code>grandParent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Grand Parent"</span>))
parent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Parent"</span>),<span class="hljs-literal">true</span>)
target.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Target"</span>))
</code></pre><p>What would be the logged on the console on clicking the target(smallest) div now?</p>
<iframe src="https://codesandbox.io/embed/capturing-then-bubbling-sgv74o?expanddevtools=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<p>on clicking the target div(smallest div) :</p>
<ul>
<li>We get Parent =&gt; Target =&gt; Grandparent logged in the console.</li>
<li>To predict this output we need to remember one simple thing -</li>
</ul>
<blockquote>
<p>In an event propagation cycle, the <em>capturing</em> phase occurs first then followed by the <em>target</em> phase and <em>bubbling</em> phase.</p>
</blockquote>
<p>Consider the image below - </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1652301567630/pNHZjJ2GD.png" alt="Event Capturing or Event Trickling (1).png" class="image--center mx-auto" /></p>
<ul>
<li>The event propagation happens down the mountain and then up the mountain.</li>
<li>Events are placed on the vertical plane based on the hierarchy in the DOM. i.e. Grandparent contains the parent so the grandparent will be above the parent.</li>
<li>Also, based on the true/false attribute of the capture flag events are placed on the capture line or bubble line</li>
<li>Now we traverse as per the arrows shown and we get the expected output.</li>
</ul>
<h2 id="heading-how-to-stop-this-propagation">How to stop this propagation?</h2>
<p>So far we saw an event propagating either up or down the DOM tree but what if we only want the event to happen on the target div and do not propagate, say we want to log on which div the user clicked, here we could use something as: <code>e.stopPropagation()</code></p>
<pre><code>grandParent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Grand Parent"</span>))
parent.addEventListener(<span class="hljs-string">"click"</span>,() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(<span class="hljs-string">"Parent"</span>))
target.addEventListener(
  <span class="hljs-string">"click"</span>,
  (e) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
    e.stopPropagation();
    console.log(<span class="hljs-string">"Target"</span>);
  }
)
</code></pre><p>Try clicking on target(smallest) div :</p>
<iframe src="https://codesandbox.io/embed/stoppropagation-sgv74o?expanddevtools=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<p>Here, since the target has a stopPropagation so events from the target won't propagate. i.e. on clicking on target(smallest) div we will see only <em>target</em> in the console. </p>
<p>Try clicking on the parent div now-
Since there is no such stopPropagation on the parent, clicking on the parent will still behave as it behaved before and consoled <em>parent =&gt; grandparent</em></p>
<h2 id="heading-event-delegation">Event Delegation</h2>
<p>Event delegation is a performance optimization technique. Say you have a shopping site where there are multiple categories, now if you attach onClick handlers to each category then when the page is fully loaded then it will have too many event listeners. Having too many event listeners can slow down the performance of a web app and hence event delegation comes to the rescue.</p>
<h4 id="heading-implementation">Implementation</h4>
<p>Instead of attaching event listeners to all target elements, attach an event listener to the parent and with the help of the event-bubbling get what target has been clicked.</p>
<pre><code>const parent <span class="hljs-operator">=</span> document.querySelector(<span class="hljs-string">"#parent"</span>);
parent.addEventListener(<span class="hljs-string">"click"</span>,(e) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> console.log(e.target.innerText))
</code></pre><p>Click on the numbered divs it will console the number of the div clicked</p>
<iframe src="https://codesandbox.io/embed/event-delegation-zt5dm8?expanddevtools=1&amp;fontsize=14&amp;hidenavigation=1&amp;theme=dark" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<h2 id="heading-tldr">TLDR</h2>
<p>Event Bubbling - start from the target element and traverse up in the DOM tree
Event Capturing - start from the top of the DOM tree and traverse down to the target
Event Delegation - Instead of attaching multiple events listeners attach one event listener to the parent and with the help of event bubbling get on which element the event has occurred.</p>
]]></content:encoded></item><item><title><![CDATA[Debouncing and Throttling]]></title><description><![CDATA[Introduction
Javascript is a single-threaded interpreted language with a non-blocking event loop.
What this means is that everything runs on a single main thread so it's important to use the thread only when required. Blocking this main thread means ...]]></description><link>https://blog.sudipkundu.com/debouncing-and-throttling</link><guid isPermaLink="true">https://blog.sudipkundu.com/debouncing-and-throttling</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[web performance]]></category><category><![CDATA[optimization]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Sudip Kundu]]></dc:creator><pubDate>Wed, 11 May 2022 13:26:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1652274671826/5b6fBjGez.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-introduction">Introduction</h1>
<p>Javascript is a single-threaded interpreted language with a non-blocking event loop.
What this means is that everything runs on a single main thread so it's important to use the thread only when required. Blocking this main thread means subsequent operations have to wait till the main thread is clear, this may result in performance issues.</p>
<p>In this blog, we will look at 2 simple examples to understand what are these issues &amp; how to tackle them thereby optimizing performance. Let's say we have an <em>expensive function</em>, a function that makes an API call and performs some heavy computation and then some significant UI changes.</p>
<h3 id="heading-example-1">Example 1 :</h3>
<p>Consider an input search field with this expensive function attached to its onChange handler. Say, I want to search for a "refrigerator" and I type this without making any wrong keystrokes. In this case, the expensive function will be called 12 times. You might argue that it is better to call the function as suggestions can be quicker. Now, Imagine 1 million users search something at the same time. Imagine the number of requests a server gets that too when someone doesn't make a mistake in typing. So, as the number increases, the number of requests also increases. But do we need to call this function on every change? We probably don't, we can optimize this by calling the function if the delay between two events(keystrokes) is more than let's say 400ms. In simple words, if the user pauses while typing call the function. This method of performance optimization is called debouncing.</p>
<h4 id="heading-implementation">Implementation :</h4>
<ul>
<li>Create a function <em>debounce </em>that takes a function and a delay. </li>
<li>It returns a function that clears the timeout if it already exists and then starts a new timer of the given delay.</li>
<li>If another event occurs before the timer is over then this function is called again the old-timer gets cleared and the new timer starts. </li>
<li>If another event does not occur before the timer gets over then the expensive function is called.</li>
</ul>
<pre><code><span class="hljs-keyword">const</span> debounce = <span class="hljs-function">(<span class="hljs-params">func, delay</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> timer;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      <span class="hljs-built_in">clearTimeout</span>(timer);
      timer = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> func(), delay);
    };
  };
</code></pre><p>Try searching with and without debounce and notice the number of API calls -</p>
<iframe src="https://codesandbox.io/embed/debouncing-blog-j8wf47?fontsize=14&amp;hidenavigation=1&amp;theme=dark&amp;view=preview" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>

<h3 id="heading-example-2">Example 2:</h3>
<p>Consider a shooting game where to fire a weapon we need to click and this expensive function is attached to its onClick handler. In an intense fighting situation where we want to kill the enemies, we would spam the clicks to fire as many rounds as possible. But, a weapon has its limits - Let's say between subsequent shots, there is a delay of 300ms. But, we won't think of all this we would spam the click. So we need to call the function only where there is a delay of 400ms from the last function call. This method of rate-limiting of the function call is called throttling.</p>
<h4 id="heading-implementation">Implementation :</h4>
<ul>
<li>Create a function <em>throttle</em> that takes a function and a delay.</li>
<li>It returns a function that runs the expensive function only if the flag is true, then set's the flag to false and starts a setTimeout to set the flag to true again after the specified limit.</li>
<li>If other events occur before this setTimeout expires then the flag is false and the expensive function is not called.</li>
<li>In this way the expensive function is only called when the flag is true, else it has to wait till the setTimeout expires and set's the flag to true</li>
</ul>
<pre><code>const throttle <span class="hljs-operator">=</span> (func, limit) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
  let flag <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">if</span> (flag) {
      func();
      flag <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;
      setTimeout(() <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> (flag <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>), limit);
    }
  };
};
</code></pre><p>Try Spamming the two fire buttons and notice the number of shots fired - </p>
<iframe src="https://codesandbox.io/embed/throttling-blog-hqyuzt?fontsize=14&amp;hidenavigation=1&amp;theme=dark&amp;view=preview" style="width:100%;height:500px;border:0;border-radius:4px;overflow:hidden" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>


<h3 id="heading-tldr">TLDR</h3>
<p>DEBOUNCING =&gt; call a function if the delay between <em>events </em>is more than d
THROTTLING  =&gt; call a function if the delay between <em>function call</em> is more than d</p>
<p>Note: When it comes to debouncing vs throttling, there is no clear winner, it depends on the situation.</p>
]]></content:encoded></item><item><title><![CDATA[Render > Reconciliation > React]]></title><description><![CDATA[Introduction
One of the reasons React is so popular is that it's blazing fast. This speed is achieved by updating only part of the real DOM that has changed. But, updating the real DOM is a slow process. So, how does React achieve this? React achieve...]]></description><link>https://blog.sudipkundu.com/render-reconciliation-react</link><guid isPermaLink="true">https://blog.sudipkundu.com/render-reconciliation-react</guid><category><![CDATA[React]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Sudip Kundu]]></dc:creator><pubDate>Thu, 10 Mar 2022 06:25:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1646892536940/CXmDB-zAt.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>One of the reasons React is so popular is that it's blazing fast. This speed is achieved by updating only part of the real DOM that has changed. But, updating the real DOM is a slow process. So, how does React achieve this? React achieves this using virtual DOM.</p>
<p>Virtual DOM is a lighter version of real DOM. But, it looks nowhere like the real DOM instead it's a tree of React(JS) objects that represent the UI at an instant.</p>
<blockquote>
<p>Think of the real DOM as a building and virtual DOM as a blueprint of the building.</p>
</blockquote>
<p>When a state changes or prop updates, it triggers a render.</p>
<p>When we hear the term "render" we think of it as <em>painting a picture</em> or <em>printing a page</em>. However, in React this is not what "render" means!</p>
<h3 id="heading-render">Render</h3>
<p>This is a process of traversing through the virtual DOM and collecting the updated states or props and running the <code>render()</code> function for those components which use the updated state or prop and create the new virtual DOM.</p>
<h3 id="heading-reconciliation">Reconciliation</h3>
<p>This process involves comparing the new virtual DOM with the old virtual DOM and finding the differences. This process is called <a target="_blank" href="https://reactjs.org/docs/reconciliation.html">reconciliation</a> and it uses a <strong>diffing</strong> algorithm based on two assumptions:-</p>
<ol>
<li>Two elements of different types will produce different trees.</li>
<li>The developer can hint at which child elements may be stable across different renders with a <em>key </em>prop.</li>
</ol>
<p>Due to the 2nd assumption, you might have seen <code>Warning: Each child in a list should have a unique "key" prop</code> many times. Now you know why this happens.</p>
<p>We'll look into what the 1st assumption means in the example given later.</p>
<h3 id="heading-react">React</h3>
<p>Once React finds the most efficient way to update the UI, the real DOM gets updated.
This is called a React phase or <strong>Commit</strong> phase (not render phase).</p>
<hr />
<h3 id="heading-lets-understand-this-better-with-an-example">Let's understand this better with an example:</h3>
<p>Throughout this example, the UI will remain the same, only the code will change.</p>
<h4 id="heading-part-1">Part 1</h4>
<ul>
<li>We have a simple counter with 2 buttons increment and decrement.</li>
<li>We also have a div that changes style at the click of a button.</li>
<li>CSB: https://codesandbox.io/embed/rrr-1-g58481</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646885394724/0APzVWCGf.gif" alt="RRR-1.gif" /></p>
<ul>
<li>on clicking increment/decrement the counter increases/decreases.</li>
<li>then, if we click on <em>Toggle Style</em> the background changes.</li>
<li>there is <strong>no effect</strong> on the counter on clicking <em>Toggle Style</em>.</li>
</ul>
<h4 id="heading-part-2">Part 2</h4>
<ul>
<li>Now, we change the code slightly for the style change div</li>
<li>CSB: https://codesandbox.io/embed/rrr-2-9dv9jb</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646886640175/H8czgY5pp.gif" alt="RRR-2.gif" /></p>
<ul>
<li>We've changed the style change div to a <strong>single component</strong> and on the click of <em>Toggle Theme</em> we see the same output as before.</li>
</ul>
<h4 id="heading-part-3">Part 3</h4>
<ul>
<li>Now, we change the code slightly more for the style change div</li>
<li>CSB: https://codesandbox.io/embed/rrr-3-ng6xrf</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646887047951/VjCxt2TMG.gif" alt="RRR-3.gif" /></p>
<ul>
<li>instead of just the style of <em>h1</em> being changed we see the whole h1 being changed and not just that we see the whole <em>example-2-wrapper</em> div and its children being changed. </li>
<li>So, what happened here is on click of <em>Toggle Style</em> the whole component is being changed and is rendered accordingly. So, looking at the real DOM, it might look like not much needs to be changed but instead of looking at the real DOM, we need to look at the virtual dom and then we see the <em>Comp1</em> being changed to <em>Comp2</em> and vice versa and hence all of its children will render.</li>
</ul>
<h4 id="heading-part-4">Part 4</h4>
<ul>
<li>Now, we change the code slightly again</li>
<li>CSB: https://codesandbox.io/embed/rrr-4-l016gq</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646888873425/W6wlIwajTZ.gif" alt="RRR-4.gif" /></p>
<ul>
<li>Now, we extracted the counter into a component keeping other things the same.</li>
<li>As excepted, the output remains the same as before</li>
</ul>
<h4 id="heading-part-5">Part 5</h4>
<ul>
<li>Now, we make some more code changes</li>
<li>CSB: https://codesandbox.io/embed/rrr-5-2t6t72</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646889619644/1BRO7XiAl.gif" alt="RRR-5.gif" /></p>
<ul>
<li>On clicking <em>Toggle Style</em> we see the counter getting reset.</li>
<li><p>So, what's happened here is, the same that happened in <em>Part 3</em>. On clicking <em>Toggle Style</em> a new component gets rendered and hence all of its children get rendered freshly.</p>
</li>
<li><p>In case, we need to make the counter retain its state then we may use context</p>
</li>
</ul>
<p>The way React sees while diffing:</p>
<ul>
<li>Node is same -&gt; React goes inside</li>
<li>Node is different -&gt; Change fully</li>
</ul>
<p>This was my understanding of Render Reconciliation React.</p>
]]></content:encoded></item></channel></rss>