<?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: Akshay</title>
    <description>The latest articles on DEV Community by Akshay (@akshayone8).</description>
    <link>https://dev.to/akshayone8</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%2F1703463%2F11e99574-8096-4aeb-886c-6aaae16a264f.jpeg</url>
      <title>DEV Community: Akshay</title>
      <link>https://dev.to/akshayone8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akshayone8"/>
    <language>en</language>
    <item>
      <title>🍽️ Why You Don’t Need 20 Waiters in a Restaurant (👨‍🍳 = JavaScript Insight)</title>
      <dc:creator>Akshay</dc:creator>
      <pubDate>Mon, 19 May 2025 07:56:28 +0000</pubDate>
      <link>https://dev.to/akshayone8/why-you-dont-need-20-waiters-in-a-restaurant-javascript-insight-2hn6</link>
      <guid>https://dev.to/akshayone8/why-you-dont-need-20-waiters-in-a-restaurant-javascript-insight-2hn6</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Understanding event bubbling, event capturing, and why events exist at all — with restaurant and classroom stories you’ll never forget.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Events Are Needed
📱Imagine a food delivery app.
You click the &lt;strong&gt;“Order Now”&lt;/strong&gt; button… and nothing happens🤷‍♂️.
No sound. No feedback. No confirmation. Just dead silence.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s what happens when a page doesn’t listen for user events. Without events:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can’t respond to button clicks&lt;/li&gt;
&lt;li&gt;Can’t handle form submissions&lt;/li&gt;
&lt;li&gt;Can’t open/close modals&lt;/li&gt;
&lt;li&gt;Can’t run animations or logic&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Events are how JavaScript knows something has happened — and gives you a chance to respond.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Event Bubbling and Capturing Were Introduced?
&lt;/h2&gt;

&lt;p&gt;Let’s jump into our main metaphor:&lt;/p&gt;

&lt;h3&gt;
  
  
  🍽️ The Restaurant Waiter Story
&lt;/h3&gt;

&lt;p&gt;Scenario 1 – A waiter per table:&lt;br&gt;
Imagine a restaurant with 20 tables. To handle orders, you assign 20 waiters – one per table.&lt;/p&gt;

&lt;p&gt;Now every time a customer waves, a different waiter responds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzwnego83nc5eki1n30l8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzwnego83nc5eki1n30l8.png" alt="Image showing Restaurant Waiter Story" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🛑 Problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too much overhead&lt;/li&gt;
&lt;li&gt;Hard to manage&lt;/li&gt;
&lt;li&gt;Doesn’t scale&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  ✅ The Smart Waiter Strategy
&lt;/h3&gt;

&lt;p&gt;Now instead of 20 waiters, you hire 1 smart waiter who stands near the door.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;He watches all tables.&lt;/li&gt;
&lt;li&gt;When someone waves (clicks), he steps in and takes the order.&lt;/li&gt;
&lt;li&gt;Efficient, less chaos.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  🔁 3. What is Event Bubbling?
&lt;/h2&gt;

&lt;p&gt;*&lt;em&gt;Event bubbling means the event starts from the clicked element and moves upward in the DOM hierarchy.&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Child → Parent → Grandparent → Document&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;👨‍👩‍👧 Family Tree Example:&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;div id="grandparent"&amp;gt;
  Grandparent
  &amp;lt;div id="parent"&amp;gt;
    Parent
    &amp;lt;div id="child"&amp;gt;Child&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 Bubbling Code Example:&lt;/p&gt;

&lt;p&gt;`document.getElementById("grandparent").addEventListener("click", () =&amp;gt; {&lt;br&gt;
  console.log("Grandparent clicked");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;document.getElementById("parent").addEventListener("click", () =&amp;gt; {&lt;br&gt;
  console.log("Parent clicked");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;document.getElementById("child").addEventListener("click", () =&amp;gt; {&lt;br&gt;
  console.log("Child clicked");&lt;br&gt;
});`&lt;/p&gt;

&lt;p&gt;🧪 Output when clicking on child:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Child clicked
&amp;gt; Parent clicked
&amp;gt; Grandparent clicked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;🌊 The event bubbles up from child to grandparent.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🔄 4. What is Event Capturing?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Event capturing is the opposite direction.&lt;/strong&gt;&lt;br&gt;
The event starts from the top (document) and travels downward to the actual element.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Document → Grandparent → Parent → Child&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🧠 Capturing Code Example:&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("grandparent").addEventListener("click", () =&amp;gt; {
  console.log("Grandparent captured");
}, true); // 👈 third param true = capturing

document.getElementById("parent").addEventListener("click", () =&amp;gt; {
  console.log("Parent captured");
}, true);

document.getElementById("child").addEventListener("click", () =&amp;gt; {
  console.log("Child captured");
}, true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧪 Output when clicking on child:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Grandparent captured
Parent captured
Child captured
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;🔎 Capturing allows you to intercept or monitor events before they reach the child.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  ✋ 5. What is event.stopPropagation()?
&lt;/h2&gt;

&lt;p&gt;Sometimes, you don’t want the event to keep traveling up or down the DOM tree.&lt;/p&gt;

&lt;p&gt;Example: You want to handle the event at the child and stop it there.&lt;/p&gt;

&lt;p&gt;📌 Code Example:&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("child").addEventListener("click", (e) =&amp;gt; {
  console.log("Child clicked - Stopping propagation");
  e.stopPropagation(); // 👈 stops the event here
});

document.getElementById("parent").addEventListener("click", () =&amp;gt; {
  console.log("Parent clicked");
});

document.getElementById("grandparent").addEventListener("click", () =&amp;gt; {
  console.log("Grandparent clicked");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧪 Output when clicking child:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Child clicked - Stopping propagation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s it! The event does not bubble to parent or grandparent.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;🚦 Use stopPropagation() when:&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to prevent accidental triggers on parent elements&lt;/li&gt;
&lt;li&gt;You're handling modals, dropdowns, or stop-click-outside logic&lt;/li&gt;
&lt;li&gt;You want full control over the event path&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  💡 Summary
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Direction&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bubbling&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Child → Parent → Document&lt;/td&gt;
&lt;td&gt;Efficient event delegation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Capturing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Document → Parent → Child&lt;/td&gt;
&lt;td&gt;Early interception before the target&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;stopPropagation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stops the event mid-flow&lt;/td&gt;
&lt;td&gt;Avoid unwanted side-effects&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>programming</category>
    </item>
    <item>
      <title>🍭 Debouncing vs Throttling — Explained Through a Cute Story of a Daughter and Her Mother!</title>
      <dc:creator>Akshay</dc:creator>
      <pubDate>Wed, 23 Apr 2025 10:41:51 +0000</pubDate>
      <link>https://dev.to/akshayone8/debouncing-vs-throttling-explained-through-a-cute-story-of-a-daughter-and-her-mother-51do</link>
      <guid>https://dev.to/akshayone8/debouncing-vs-throttling-explained-through-a-cute-story-of-a-daughter-and-her-mother-51do</guid>
      <description>&lt;p&gt;Performance optimization is a key part of frontend development, especially when handling rapid user interactions like typing, scrolling, or button clicks. That’s where two powerful techniques come into play — Debouncing and Throttling.&lt;/p&gt;

&lt;p&gt;But these words sound fancy, right?&lt;/p&gt;

&lt;p&gt;So let me break them down for you in a simple story involving a little girl and her mom — a story you’ll never forget 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  👧 Debouncing — “Ask only after you stop asking!”
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;🧠 Real-World Scenario:&lt;/em&gt;&lt;br&gt;
Imagine a little girl (the user) wants a candy (API call), and she keeps bugging her mom (the system) every few seconds:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Mom, can I have candy? Can I have candy? Can I have candy?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mom gets overwhelmed. So she says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Ask me only once after you’re done talking for 5 seconds!”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, every time the girl opens her mouth again, the 5-second timer resets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally, when she stays quiet for 5 seconds, mom gives her the candy 🍬&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That’s Debouncing!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🔁 It waits for the last request, and only proceeds after the user stops asking for a while.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7yt2y1mabf3egndhfkk1.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7yt2y1mabf3egndhfkk1.jpeg" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💻 Debounce Code Example&lt;/strong&gt;&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 lang="en"&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;title&amp;gt;Debounce Example&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

  &amp;lt;textarea id="myInput" onkeyup="betterFunction()"&amp;gt;&amp;lt;/textarea&amp;gt;

  &amp;lt;script&amp;gt;
    let count = 0;

    function getData() {
      console.log('Function called ' + count++);
    }

    const myDebounce = (fn, delay) =&amp;gt; {
      let timer;
      return function(...args) {
        if (timer) clearTimeout(timer); // Clear previous request

        timer = setTimeout(() =&amp;gt; {
          fn();
        }, delay); // Wait until user stops
      };
    };

    let betterFunction = myDebounce(getData, 1000);
  &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  👧 Throttling — “You’ll get it, but only once in a while!”
&lt;/h2&gt;

&lt;p&gt;Now let’s return to our girl and her candy.&lt;/p&gt;

&lt;p&gt;This time, mom sets a rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“You’ll get only one candy every 10 minutes. Don’t keep asking!”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if the girl asks at:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Minute 1 ❌ (Ignored)&lt;/li&gt;
&lt;li&gt;Minute 3 ❌ (Ignored)&lt;/li&gt;
&lt;li&gt;Minute 7 ❌ (Ignored)&lt;/li&gt;
&lt;li&gt;Minute 10 ✅ (Gets candy!)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s Throttling!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🔁 It ensures that a function runs at most once in a specific time interval, even if it’s triggered many times.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjthjqx1zq2m227htlmfa.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjthjqx1zq2m227htlmfa.jpeg" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;💻 Throttle Polyfill Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myThrottle = (cb, delay) =&amp;gt; {
  let last = 0;
  return (...args) =&amp;gt; {
    let now = new Date().getTime();
    if (now - last &amp;lt; delay) return; // Ignore request if too soon
    last = now;
    return cb(...args);
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;🔄 Debounce vs Throttle — In a Nutshell&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjo3k01ua9zi4iqrkm091.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjo3k01ua9zi4iqrkm091.png" alt="Image description" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding State Updates in React with a Simple Marble Game.</title>
      <dc:creator>Akshay</dc:creator>
      <pubDate>Sat, 19 Apr 2025 15:26:21 +0000</pubDate>
      <link>https://dev.to/akshayone8/understanding-state-updates-in-react-with-a-simple-marble-game-2mkf</link>
      <guid>https://dev.to/akshayone8/understanding-state-updates-in-react-with-a-simple-marble-game-2mkf</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;The Game Setup&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you're playing a game where you have a bag of marbles. Each time it's your turn, you can add one more marble to your bag. Let's compare this to a React component where you have a state variable called slider, representing the number of marbles in your bag.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Scenario 1: Direct State Update&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;When you use the state update like &lt;code&gt;setSlider(slider + 1)&lt;/code&gt;, it’s similar to looking into your bag, counting the marbles you have, say five, and then deciding you will have six marbles next. You then pass this message along to a friend who is responsible for updating the marble count. If you repeat this command multiple times quickly, without waiting for confirmation from your friend, you always start from the last counted number, which was five. This means, even if you issue the update three times in a row, you might still end up thinking you should have six marbles because your friend didn’t have time to update the count between your commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Scenario 2: Functional State Update &lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;On the other hand, using &lt;code&gt;setSlider(slider =&amp;gt; slider + 1)&lt;/code&gt; is like telling your friend, Please check how many marbles are currently in the bag and add one more. In this case, you don’t do the counting; your friend does. Your friend checks the current count right before adding a new marble, ensuring each addition is based on the most recent count.This method is particularly useful in React because it handles rapid or multiple state updates based on the most current state value. It ensures that every update is accurate, no matter how quickly they are fired off.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Why This Matters&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;In web development, especially when using React, components might update frequently, and state values can change rapidly. By using the functional update form &lt;code&gt;slider =&amp;gt; slider + 1&lt;/code&gt;, you ensure that each update is dependent on the most recent state. This is crucial for avoiding bugs where your UI might not reflect the correct state due to outdated values.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Conclusion&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Understanding the difference between direct and functional state updates can greatly enhance your ability to manage state in React effectively. It helps prevent bugs and ensures that your application behaves as expected, even under rapid state changes. This simple marble game analogy helps illustrate why updating state functionally can be a better choice in many scenarios, ensuring your application remains robust and reliable.&lt;/p&gt;

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