<?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>Forem: Vivek Lagshetti</title>
    <description>The latest articles on Forem by Vivek Lagshetti (@viveklagshetti).</description>
    <link>https://forem.com/viveklagshetti</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%2F3495050%2F7077bdaf-3f97-4069-8fe4-1646a1d3d78f.jpg</url>
      <title>Forem: Vivek Lagshetti</title>
      <link>https://forem.com/viveklagshetti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/viveklagshetti"/>
    <language>en</language>
    <item>
      <title>🔥 Deep Dive into useCallback: Why Functions Break Memoization &amp; How React Fixes It</title>
      <dc:creator>Vivek Lagshetti</dc:creator>
      <pubDate>Fri, 12 Sep 2025 07:44:30 +0000</pubDate>
      <link>https://forem.com/viveklagshetti/deep-dive-into-usecallback-why-functions-break-memoization-how-react-fixes-it-2ahc</link>
      <guid>https://forem.com/viveklagshetti/deep-dive-into-usecallback-why-functions-break-memoization-how-react-fixes-it-2ahc</guid>
      <description>&lt;p&gt;If you’ve been working with React and performance optimization, you’ve probably heard of React.memo, useMemo, and useCallback.&lt;/p&gt;

&lt;p&gt;Most tutorials show you “surface-level” examples, but today we’ll go deeper into useCallback — how JavaScript handles function references, why they cause unnecessary re-renders, and how React solves it under the hood.&lt;/p&gt;

&lt;p&gt;Let’s get started. 🚀&lt;/p&gt;

&lt;h4&gt;
  
  
  🧠 The Root Problem: Functions Are Objects
&lt;/h4&gt;

&lt;p&gt;In JavaScript, functions are objects.&lt;/p&gt;

&lt;p&gt;That means every time you declare a function inside a component, a brand-new function object is created in memory on every render.&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%2F8l2p4hg7xgddbyfc25rj.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%2F8l2p4hg7xgddbyfc25rj.png" alt="Functions are Objects in Javascript" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if two functions look the same, they don’t share the same reference.&lt;/p&gt;

&lt;p&gt;👉 This becomes a problem when passing functions as props.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔴 Without useCallback: Function References Break Memoization
&lt;/h4&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%2F38e4t5u4iwetpgzclhgm.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%2F38e4t5u4iwetpgzclhgm.png" alt="Without useCallback hook Code snippet" width="800" height="838"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  👉 What happens here?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Parent re-renders whenever count changes.&lt;/li&gt;
&lt;li&gt;Each render creates a new function reference for handleClick.&lt;/li&gt;
&lt;li&gt;React.memo in Child compares props → sees onClick as changed (new reference).&lt;/li&gt;
&lt;li&gt;Child re-renders unnecessarily&lt;/li&gt;
&lt;/ul&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%2Fvd0r81fp8jyxsfmfqgi7.gif" 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%2Fvd0r81fp8jyxsfmfqgi7.gif" alt="Without useCallback hook" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  🟢 With useCallback: Stable Function Reference
&lt;/h4&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%2F6ll1oy24cn9vjy7rb3py.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%2F6ll1oy24cn9vjy7rb3py.png" alt="With useCallback hook" width="800" height="838"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the child doesn’t re-render unless it needs to. 🎉&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%2Fzg7ia8jsg6u7cs5r4pzo.gif" 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%2Fzg7ia8jsg6u7cs5r4pzo.gif" alt="With useCallback" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  ⚡ How useCallback Works Internally
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fn = useCallback(callback, deps);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;React does this internally:&lt;/li&gt;
&lt;li&gt;Checks if there’s a previous function stored in the hook state.&lt;/li&gt;
&lt;li&gt;Compares the new deps array with the old one (shallow compare)

&lt;ul&gt;
&lt;li&gt;If deps are the same → reuse the old function reference.&lt;/li&gt;
&lt;li&gt;If deps changed → create and store a new function reference.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCallback(fn, deps) {
  const last = getHookState(); // { fn, deps }
  if (last &amp;amp;&amp;amp; shallowEqual(last.deps, deps)) {
    return last.fn; // reuse reference
  }
  setHookState({ fn, deps });
  return fn; // new reference
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So useCallback is basically a function reference cache.&lt;/p&gt;

&lt;h4&gt;
  
  
  🎯 The Mystery of the Empty Array []
&lt;/h4&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fn = useCallback(() =&amp;gt; {
  console.log("Hello");
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;[] → no dependencies.&lt;/li&gt;
&lt;li&gt;React creates the function once and always returns the same reference.&lt;/li&gt;
&lt;li&gt;Perfect for callbacks that don’t depend on any state/props.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But ⚠️ watch out for the stale closure trap.&lt;/p&gt;

&lt;h4&gt;
  
  
  🕳️ The Closure Trap
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [count, setCount] = useState(0);

const logCount = useCallback(() =&amp;gt; {
  console.log(count);
}, []); // empty deps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;First render: count = 0.&lt;/li&gt;
&lt;li&gt;Function is created with count = 0.&lt;/li&gt;
&lt;li&gt;Even when count updates → function still closes over old value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So logs will always show 0.&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ Fix by adding dependencies:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const logCount = useCallback(() =&amp;gt; {
  console.log(count);
}, [count]); // updates with count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the function is recreated whenever count changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  ⚖️ When to Use useCallback
&lt;/h4&gt;

&lt;p&gt;✅ When passing functions to React.memo child components.&lt;br&gt;
✅ When functions are dependencies of other hooks (useEffect, useMemo).&lt;br&gt;
❌ Don’t wrap every function blindly → unnecessary overhead.&lt;/p&gt;

&lt;h4&gt;
  
  
  🚀 Final Recap
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Functions in JS are objects → new reference each render.&lt;/li&gt;
&lt;li&gt;React compares references, not contents.&lt;/li&gt;
&lt;li&gt;useCallback stabilizes function references until dependencies change.&lt;/li&gt;
&lt;li&gt;[] = never change (but beware stale closures).&lt;/li&gt;
&lt;li&gt;Works best with React.memo to prevent child re-renders.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;🔗 If you’ve mastered useMemo and React.memo, useCallback is the missing piece that completes the optimization trio.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;👉 Next time your child keeps re-rendering even though “nothing changed” — check if you forgot useCallback.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>learning</category>
      <category>react</category>
    </item>
    <item>
      <title>⚡ Optimizing React Performance with React.memo (Real-Time Example)</title>
      <dc:creator>Vivek Lagshetti</dc:creator>
      <pubDate>Fri, 12 Sep 2025 06:15:36 +0000</pubDate>
      <link>https://forem.com/viveklagshetti/optimizing-react-performance-with-reactmemo-real-time-example-43gg</link>
      <guid>https://forem.com/viveklagshetti/optimizing-react-performance-with-reactmemo-real-time-example-43gg</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;We often hear about useMemo, useCallback, and React.memo — but what exactly does React.memo do, and when should you use it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s explore with a real-time example using a Parent → Child component scenario.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔴 Without React.memo
&lt;/h4&gt;

&lt;p&gt;Here’s a simple setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parent has two states: count and text&lt;/li&gt;
&lt;li&gt;Child always receives a static prop (value="Static Prop")&lt;/li&gt;
&lt;/ul&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%2Ffn54x0pm3nazqbxkruk7.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%2Ffn54x0pm3nazqbxkruk7.png" alt="Without React.memo" width="800" height="816"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  👉 Problem:
&lt;/h4&gt;

&lt;p&gt;Whenever you click Increase Count, the child re-renders unnecessarily. You can confirm this by checking the console logs.&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%2F34az8j27x9h373lw9k6g.gif" 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%2F34az8j27x9h373lw9k6g.gif" alt="Without using React.memo" width="700" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  🟢 With React.memo
&lt;/h4&gt;

&lt;p&gt;Now let’s wrap our Child with React.memo.&lt;/p&gt;

&lt;p&gt;This tells React:&lt;/p&gt;

&lt;p&gt;“Only re-render this component if its props actually change.”&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%2Fxldxcirdzl12oz850sk3.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%2Fxldxcirdzl12oz850sk3.png" alt="With using React.memo" width="800" height="824"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Now, when you click Increase Count,&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%2Fvjlbioc3rcqqdfh1fk2x.gif" 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%2Fvjlbioc3rcqqdfh1fk2x.gif" alt="With using React.memo" width="700" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Parent re-renders (as expected)&lt;/li&gt;
&lt;li&gt;But the Child does NOT re-render, since its props remain the same&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🎉 Check the console — you’ll see that the child only renders once, no matter how many times you increase the count.&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ Advantages of React.memo
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Prevents unnecessary re-renders of child components&lt;/li&gt;
&lt;li&gt;Improves performance in large component trees&lt;/li&gt;
&lt;li&gt;Works best with pure components (output depends only on props)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  ⚡ Key Difference (React.memo vs useMemo)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;useMemo → Memoizes values/calculations inside a component&lt;/li&gt;
&lt;li&gt;React.memo → Memoizes entire components to skip re-rendering when props don’t change&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🚀 Takeaway
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;If you’re passing static props (or props that rarely change) to child components, React.memo is a simple and powerful way to avoid wasteful re-renders.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;💡 What about you? Do you use React.memo in your React projects? Have you ever faced unnecessary re-render issues? Let’s discuss!&lt;/p&gt;

&lt;p&gt;Even though the child’s props never change, it still re-renders whenever the parent re-renders.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🚀 Optimizing React Performance with useMemo Hook (Real-Time Example)</title>
      <dc:creator>Vivek Lagshetti</dc:creator>
      <pubDate>Fri, 12 Sep 2025 05:52:56 +0000</pubDate>
      <link>https://forem.com/viveklagshetti/optimizing-react-performance-with-usememo-hook-real-time-example-1l22</link>
      <guid>https://forem.com/viveklagshetti/optimizing-react-performance-with-usememo-hook-real-time-example-1l22</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;When building React apps, performance optimization often feels like an afterthought — until your app starts lagging. One common issue is expensive re-calculations happening on every render, even when they’re not needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this post, I’ll show you a real-time example of how useMemo can help us optimize our app with proof by comparing two components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Without useMemo ❌ → Filtering happens on every render&lt;/li&gt;
&lt;li&gt;With useMemo ✅ → Filtering only happens when search changes&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🐢 Without useMemo
&lt;/h4&gt;

&lt;p&gt;Here’s a simple example where we have 5,000 users and a search bar.&lt;/p&gt;

&lt;p&gt;Every time we type something or even click a button unrelated to the search, React re-runs the filtering logic across all 5,000 users.&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%2F85vbci2wbld61w8ruuzc.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%2F85vbci2wbld61w8ruuzc.png" alt="Without useMemo" width="800" height="962"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Try clicking the Increase Count button — even though it has nothing to do with the search, the filter runs again, wasting CPU cycles.&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%2F39op0210ja4bha59y9lu.gif" 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%2F39op0210ja4bha59y9lu.gif" alt="Without using useMemo" width="400" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  ⚡ With useMemo
&lt;/h4&gt;

&lt;p&gt;Now, let’s optimize it with useMemo.&lt;/p&gt;

&lt;p&gt;We’ll memoize the filtered list so that React only recomputes when search changes, not when unrelated state (count) changes.&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%2Fdglckdqwn8re37b7j6ds.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%2Fdglckdqwn8re37b7j6ds.png" alt="With useMemo" width="800" height="1203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Now, open the console and try again. You’ll notice “Filtering users…” only logs when you change the search input, not when you click the button. 🎉&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%2F2xfsocyrfrlybkkinr08.gif" 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%2F2xfsocyrfrlybkkinr08.gif" alt="With using useMemo" width="600" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  🧠 When to Use useMemo
&lt;/h4&gt;

&lt;p&gt;✅ Expensive computations (like filtering, sorting, complex calculations)&lt;/p&gt;

&lt;p&gt;✅ When you notice re-renders impacting performance&lt;/p&gt;

&lt;p&gt;✅ For lists, tables, or search-heavy components&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Don’t overuse it — useMemo itself adds a small overhead. Use it where you know recalculations are costly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  🚀 Key Takeaway
&lt;/h3&gt;

&lt;p&gt;With just one hook (useMemo), we avoided unnecessary computations and made our app more efficient.&lt;/p&gt;

&lt;p&gt;Performance optimizations like this might seem small, but at scale (large data, heavy UI), they make a huge difference.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 What about you? Have you faced performance issues in your React apps? Did useMemo or useCallback help you fix them?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;👇 Drop your thoughts in the comments!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>learning</category>
      <category>react</category>
    </item>
    <item>
      <title>Understanding Event Delegation in React: Before &amp; After</title>
      <dc:creator>Vivek Lagshetti</dc:creator>
      <pubDate>Thu, 11 Sep 2025 13:10:38 +0000</pubDate>
      <link>https://forem.com/viveklagshetti/understanding-event-delegation-in-react-before-after-4g3g</link>
      <guid>https://forem.com/viveklagshetti/understanding-event-delegation-in-react-before-after-4g3g</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;When we build web applications, handling user interactions (like clicks, key presses, or form submissions) is a crucial part of the job. But the way we handle these events has changed a lot over the years. One of the key improvements is something called event delegation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s walk through this concept step by step — starting from how we used to do it before delegation existed, and how React improved it.&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 Before Event Delegation: Direct Event Binding
&lt;/h2&gt;

&lt;p&gt;In the early days of JavaScript, if you wanted to make multiple buttons respond to clicks, the straightforward way was to attach an event listener directly to each button.&lt;br&gt;
Example:&lt;br&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%2F9zm387qcscmgms5bqf5m.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%2F9zm387qcscmgms5bqf5m.png" alt="Direct Event Binding" width="800" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  👉 What this means:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Every element gets its own event listener.&lt;/li&gt;
&lt;li&gt;If you had 1,000 buttons, you’d end up with 1,000 listeners in memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ⚠️ Problems with this old method:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Performance issues: Too many listeners slow down the page.&lt;/li&gt;
&lt;li&gt;Memory overhead: Each listener takes space.&lt;/li&gt;
&lt;li&gt;Maintenance headache: If you remove an element, you also need to remove its listener.&lt;/li&gt;
&lt;li&gt;Cross-browser quirks: Different browsers handled events differently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Enter Event Delegation
&lt;/h4&gt;

&lt;p&gt;Here’s where event delegation comes in. Instead of attaching listeners to every element, you attach one listener to a parent element (or even the document).&lt;/p&gt;

&lt;h4&gt;
  
  
  This works because of event bubbling:
&lt;/h4&gt;

&lt;p&gt;When you click a child element, the event travels (or “bubbles up”) through its ancestors until it reaches the root.&lt;br&gt;
The parent can “catch” that event and figure out which child triggered it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&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%2Fsj2nf1yfjp1hnhfgbqyt.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%2Fsj2nf1yfjp1hnhfgbqyt.png" alt="Event Delegation" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  👉 What changed:
&lt;/h4&gt;

&lt;p&gt;One single event listener on the parent handles all child button clicks.&lt;br&gt;
Adding or removing buttons doesn’t require attaching/removing new listeners.&lt;/p&gt;

&lt;h4&gt;
  
  
  ✅ Improvements:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Better performance.&lt;/li&gt;
&lt;li&gt;Less memory usage.&lt;/li&gt;
&lt;li&gt;Easier DOM updates.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  📍 React’s Approach: Synthetic Events + Delegation
&lt;/h2&gt;

&lt;p&gt;React takes this idea further. When you write:&lt;br&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%2F3tv4kmy7ymlta8dvle96.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%2F3tv4kmy7ymlta8dvle96.png" alt="React Event Delegation" width="800" height="300"&gt;&lt;/a&gt;&lt;br&gt;
…it looks like you’re attaching a listener directly to the button. But under the hood, React does not put a listener on every button.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔧 What React does instead:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;React attaches a single listener (per event type, like click) at the root level.&lt;/li&gt;
&lt;li&gt;When a real DOM event happens, React intercepts it.&lt;/li&gt;
&lt;li&gt;React creates a SyntheticEvent — a wrapper that normalizes browser differences (so you don’t worry about inconsistencies like event.target vs event.srcElement).&lt;/li&gt;
&lt;li&gt;React dispatches the event internally to the correct component’s onClick handler.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🚀 Final Thoughts
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Event delegation transformed how we handle user interactions. Instead of cluttering every element with its own event listener, we now rely on bubbling and centralized listeners.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React supercharged this by introducing its Synthetic Event system, making event handling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster&lt;/li&gt;
&lt;li&gt;Safer across browsers&lt;/li&gt;
&lt;li&gt;Easier to maintain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So next time you write an onClick in React, remember—it’s not attached to your button directly. React is quietly handling it for you through delegation.&lt;/p&gt;

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