<?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: Akash Kumawat</title>
    <description>The latest articles on Forem by Akash Kumawat (@_akashkmt_).</description>
    <link>https://forem.com/_akashkmt_</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%2F2069926%2Ff2403e99-4396-412c-9660-d65c5fb1444c.jpg</url>
      <title>Forem: Akash Kumawat</title>
      <link>https://forem.com/_akashkmt_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/_akashkmt_"/>
    <language>en</language>
    <item>
      <title>useCallback: Keeping Your Functions in Check, Literally 🧑‍💻</title>
      <dc:creator>Akash Kumawat</dc:creator>
      <pubDate>Sun, 13 Oct 2024 05:18:31 +0000</pubDate>
      <link>https://forem.com/_akashkmt_/usecallback-keeping-your-functions-in-check-literally-2eaa</link>
      <guid>https://forem.com/_akashkmt_/usecallback-keeping-your-functions-in-check-literally-2eaa</guid>
      <description>&lt;p&gt;You know the feeling — everything’s working great, but then, your app starts slowing down, and you’re left wondering: "Why, React? Why are you like this?" One of the sneaky culprits? React loves recreating functions every time it re-renders a component, like it’s some kind of hobby. But here comes &lt;code&gt;useCallback&lt;/code&gt; to save the day, keeping your functions in line and making sure React doesn’t go overboard.&lt;/p&gt;

&lt;p&gt;Let’s break it down!&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Does React Keep Making New Functions? 🤔
&lt;/h2&gt;

&lt;p&gt;So here’s the deal. Every time React re-renders a component, it creates brand-new functions — even if those functions do the exact same thing as before. This happens because React doesn’t remember function references between renders. Every single time React runs, it's like, "Let’s create a fresh one, just in case."&lt;/p&gt;

&lt;p&gt;Seems harmless, right? But it can cause performance issues, especially when you’re passing those new functions down to child components. React thinks, “Oh, new function? Must be new props!”—and then it triggers a re-render of the child component, even when it doesn’t need to.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enter &lt;code&gt;useCallback&lt;/code&gt;: Your Function Stabilizer 🦸‍♂️
&lt;/h2&gt;

&lt;p&gt;This is where useCallback steps in like the hero React didn’t know it needed. Instead of creating new functions on every render, useCallback lets you say, “Hey React, unless something important changes, just keep using the same function.”&lt;/p&gt;

&lt;p&gt;How does it work? Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunction = useCallback(() =&amp;gt; {
  // Logic goes here
}, [dependencies]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;dependencies&lt;/code&gt; array is the key. React will only create a new version of the function if something in the array changes. Otherwise, it’s the same trusty function from the last render. Smooth sailing from here! 🚢&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Call in &lt;code&gt;useCallback&lt;/code&gt; ⚙️
&lt;/h2&gt;

&lt;p&gt;Here are some situations where useCallback really shines:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Passing Functions to Child Components
&lt;/h3&gt;

&lt;p&gt;If you’re passing a function as a prop to a child component, React will recreate that function every time the parent re-renders. This means the child component will also re-render because React thinks the function prop is different.&lt;/p&gt;

&lt;p&gt;With useCallback, you can tell React to use the same function unless something in the dependencies changes. No more unnecessary re-renders for your child components!&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Memoized Components + Functions
&lt;/h3&gt;

&lt;p&gt;Using React.memo to prevent unnecessary re-renders? Awesome. But if you’re passing a fresh function every time, you’re still gonna get those re-renders. Pair &lt;code&gt;React.memo&lt;/code&gt; with &lt;code&gt;useCallback&lt;/code&gt;, and you’ve got yourself a super team to block unwanted re-renders.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Event Handlers that Depend on State
&lt;/h3&gt;

&lt;p&gt;Imagine you’ve got a button with an onClick handler that relies on state. Normally, React will recreate that handler every time the state updates. But with &lt;code&gt;useCallback&lt;/code&gt;, you’re telling React, “Hey, no need to recreate this function unless the state it depends on actually changes.”&lt;/p&gt;




&lt;h2&gt;
  
  
  But Don’t Go Overboard 🚨
&lt;/h2&gt;

&lt;p&gt;Now, before you wrap &lt;code&gt;useCallback&lt;/code&gt; around every function like it’s the answer to life, the universe, and everything, here’s the deal: Not every function needs it. If your component doesn’t re-render often or the performance gains are negligible, you’re better off skipping &lt;code&gt;useCallback&lt;/code&gt;. In fact, using it unnecessarily can make your code more complicated and even take up extra memory.&lt;/p&gt;

&lt;p&gt;Use it where it counts — like with functions that are passed down as props or in heavy components that need some optimization love.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping it Up 🎁
&lt;/h2&gt;

&lt;p&gt;At the end of the day, &lt;code&gt;useCallback&lt;/code&gt; is like your function stabilizer, making sure React doesn’t go wild recreating functions on every render. It’s a lifesaver when you’re trying to optimize performance, but it’s not a tool for every situation. Use it where it makes sense, and your app will thank you for it.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;useCallback&lt;/code&gt;, your functions don’t have to be the cause of unnecessary re-renders anymore. It’s time to get those functions under control! 😎&lt;/p&gt;




&lt;h3&gt;
  
  
  Happy Coding :))
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React.memo: The Bouncer for Unnecessary Re-renders 💃</title>
      <dc:creator>Akash Kumawat</dc:creator>
      <pubDate>Mon, 07 Oct 2024 18:23:16 +0000</pubDate>
      <link>https://forem.com/_akashkmt_/reactmemo-the-bouncer-for-unnecessary-re-renders-14ke</link>
      <guid>https://forem.com/_akashkmt_/reactmemo-the-bouncer-for-unnecessary-re-renders-14ke</guid>
      <description>&lt;p&gt;Imagine your React app is like a busy nightclub. Everything’s going great — music’s bumping, people are having fun—until suddenly, the crowd grows, and things slow down. You’re thinking, “Whoa, this party’s getting out of hand!” That’s when &lt;code&gt;React.memo&lt;/code&gt; steps in, acting like the club bouncer. It only lets in the folks (or props) that actually need to be there, keeping the party smooth without the unnecessary chaos of extra re-renders.&lt;/p&gt;

&lt;p&gt;Let’s check out how &lt;code&gt;React.memo&lt;/code&gt; works its magic and when it’s time to call in this re-rendering bouncer!&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting Up the Velvet Rope with React.memo 🛑
&lt;/h2&gt;

&lt;p&gt;By default, React’s like the friend who invites everyone to the party—re-rendering components left and right, just to make sure everything’s updated. Even if nothing’s really changed, React’s still like, “Better safe than sorry, let’s re-render!” 😅&lt;/p&gt;

&lt;p&gt;That’s where &lt;code&gt;React.memo&lt;/code&gt; comes in. It’s like, “Hold up! If the props haven’t changed, you’re not getting in. No need for a re-render.”&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CoolComponent = React.memo((props) =&amp;gt; {
  // Your awesome component logic here
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, with &lt;code&gt;React.memo&lt;/code&gt;, your component won’t re-render unless its props actually change. No more extra partygoers slowing down the fun! 🎉&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use React.memo Like a Boss 💼
&lt;/h2&gt;

&lt;p&gt;So, when should you bring in &lt;code&gt;React.memo&lt;/code&gt;? The golden rule: use it when you’ve got components that rely heavily on props and don’t need to re-render unless those props change.&lt;/p&gt;

&lt;p&gt;But hey, don’t go wild! &lt;code&gt;React.memo&lt;/code&gt; isn’t meant for every single component. If your component is small, has simple props, or doesn’t re-render often, you can skip it. React already knows what it’s doing most of the time. 🧘‍♂️&lt;/p&gt;

&lt;p&gt;Here’s when &lt;code&gt;React.memo&lt;/code&gt; really shines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pure components: Components that only rely on props to render.&lt;/li&gt;
&lt;li&gt;Expensive components: If rendering takes a lot of effort, like heavy calculations or complex UI elements.&lt;/li&gt;
&lt;li&gt;Lists or grids: When you have items that don’t change often. Let those list items sit back and relax without re-rendering constantly.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Kryptonite: Where React.memo Might Let You Down 🦸‍♂️💔
&lt;/h2&gt;

&lt;p&gt;Even the best bouncers can make mistakes, and &lt;code&gt;React.memo&lt;/code&gt; is no different. It has a few weaknesses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Objects and arrays as props: &lt;code&gt;React.memo&lt;/code&gt; doesn’t do a deep dive into your objects and arrays—it just checks the reference. So, if you create a new array or object on every render (even if it’s identical), React.memo will think it’s different and still re-render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pro tip&lt;/strong&gt;: Use &lt;code&gt;useCallback&lt;/code&gt; or &lt;code&gt;useMemo&lt;/code&gt; to prevent new object or array references from being created unless necessary.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heavy logic inside your component: If your component does a ton of calculations or has expensive logic, React.memo might not help much. Even if it skips a render, you’ll still pay the price of that logic running. In cases like this, you may want to rethink where your logic lives.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Don’t Overdo It! 🧘‍♀️
&lt;/h2&gt;

&lt;p&gt;It might be tempting to wrap every component in &lt;code&gt;React.memo&lt;/code&gt; to speed things up, but honestly? That’s overkill. If your component is simple and doesn’t suffer from frequent re-renders, &lt;code&gt;React.memo&lt;/code&gt; can actually slow things down a bit because it has to check the props every time.&lt;/p&gt;

&lt;p&gt;Remember: React is already pretty smart at handling updates, so you don’t need to throw React.memo everywhere. Use it wisely, like a VIP pass for your more complex or frequently updated components.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping It Up 🎁
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;React.memo&lt;/code&gt; is like the perfect party bouncer — stopping those unnecessary re-renders from getting into your component and messing things up. But like any good bouncer, it’s important to know when you actually need it. For most components, React’s got things covered, but when you need that extra bit of control, React.memo is your go-to.&lt;/p&gt;

&lt;p&gt;So, next time your app starts feeling a little sluggish, check out &lt;code&gt;React.memo&lt;/code&gt; to keep things running smoothly — and maybe even a bit cooler. 😎&lt;/p&gt;

&lt;p&gt;Stay tuned for more tips on managing re-renders like a pro! 🎉&lt;/p&gt;




&lt;h3&gt;
  
  
  Happy Coding :))
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>performance</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why So Many Re-renders, React? 🤔</title>
      <dc:creator>Akash Kumawat</dc:creator>
      <pubDate>Wed, 02 Oct 2024 14:21:07 +0000</pubDate>
      <link>https://forem.com/_akashkmt_/why-so-many-re-renders-react-3b6b</link>
      <guid>https://forem.com/_akashkmt_/why-so-many-re-renders-react-3b6b</guid>
      <description>&lt;p&gt;We’ve all been there — your React app is running smoothly, you’re feeling good, and then... BAM! Suddenly, everything slows down and you’re thinking, “What’s going on here?” 🧐 Unnecessary re-renders, my friend! They’re like those unexpected guests at a party: no one invited them, but here they are, eating your snacks and slowing everything down.&lt;/p&gt;

&lt;p&gt;Let’s break down why React loves to re-render (a little too much sometimes) and what’s really going on under the hood.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. State Changes: The Serial Re-render Offender
&lt;/h2&gt;

&lt;p&gt;Whenever you update state in React, it’s like hitting refresh on your whole component. React’s like, “Oh, you changed something? Let me just re-render EVERYTHING.” Even if it’s a tiny update, React is all in. It’s like the over-enthusiastic friend who keeps tidying up the room while you’re still hanging out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



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

const increment = () =&amp;gt; setCount(count + 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time you hit &lt;code&gt;setCount&lt;/code&gt;, React’s like, “Let’s re-render the whole component, just in case!” Sure, it means your state’s always up-to-date, but all this re-rendering can lead to performance hits if your app is doing some heavy lifting. It's like overreacting to a small spill by cleaning the entire house. 🧹&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Props Change: React’s Like, “New Props, Who Dis?”
&lt;/h2&gt;

&lt;p&gt;React is super reactive (no pun intended) to prop changes. The moment a prop changes, React goes, “Wait, is that a new prop? Better re-render the child component, just to be safe.” Even if the child doesn’t care about the new prop, React re-renders it like it’s the end of the world.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ChildComponent data={someArray} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever &lt;code&gt;someArray&lt;/code&gt; changes (even if it’s just adding one item), React’s like, “Time to re-render the whole &lt;code&gt;ChildComponent&lt;/code&gt;!” It’s basically that one overprotective friend who jumps at every minor inconvenience: “Oh, new data? Let’s redo everything!” 🙃&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Parent Component Re-renders: The Domino Effect
&lt;/h2&gt;

&lt;p&gt;This is the ultimate React drama. If the parent component re-renders, React drags all the kids along for the ride, even if they had nothing to do with the change. It’s like inviting yourself to a party and bringing a bunch of friends who didn’t even want to come. 😅&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ParentComponent() {
  const [parentState, setParentState] = useState(0);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;ChildComponent /&amp;gt;
      &amp;lt;AnotherChildComponent /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time the parent re-renders, both &lt;code&gt;ChildComponent&lt;/code&gt; and &lt;code&gt;AnotherChildComponent&lt;/code&gt; are like, “Wait, what did we do? Why are we here?” This can be fine for small apps, but in a larger, more complex app, this over-enthusiasm can really slow things down.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Inline Functions &amp;amp; Objects: The Sneaky Culprits
&lt;/h2&gt;

&lt;p&gt;Inline functions and objects might look innocent, but they're secretly wreaking havoc. Every time React re-renders, it creates new instances of these functions and objects, even if they’re technically doing the same thing. React looks at them and goes, “New reference? Must be different. Better re-render!”&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ChildComponent onClick={() =&amp;gt; doSomething()} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each time the parent re-renders, React sees the new inline function as a completely new prop for &lt;code&gt;ChildComponent&lt;/code&gt;. It’s like you just gave React a shiny new toy, and it has no idea that the toy is actually the same one it had before. So it re-renders. React’s just trying its best, okay? 😅&lt;/p&gt;




&lt;h2&gt;
  
  
  5. React’s Motto: Better Safe Than Sorry
&lt;/h2&gt;

&lt;p&gt;React’s philosophy is pretty much “better safe than sorry.” It would rather re-render too much than risk missing something important. It’s like that overly cautious friend who triple-checks everything before heading out the door. This keeps the UI in sync with the state, but can lead to React doing way more than it needs to.&lt;/p&gt;




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

&lt;p&gt;So, why all the re-renders? It’s because React just wants everything to stay updated and correct. But sometimes, it can be a little too cautious, leading to unnecessary performance issues. Don’t worry, though! We’ve got plenty of tricks up our sleeves to tame React’s re-rendering habits — like &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;useCallback&lt;/code&gt;, and other optimization tools.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next part, where we dive into the hacks to calm React down and stop the re-renders from crashing your app's party. 🎉&lt;/p&gt;




&lt;h3&gt;
  
  
  Happy Coding :))
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
      <category>frontend</category>
    </item>
    <item>
      <title>React Re-render Saga: Stopping the Madness, One Trick at a Time 🤯</title>
      <dc:creator>Akash Kumawat</dc:creator>
      <pubDate>Thu, 26 Sep 2024 13:01:19 +0000</pubDate>
      <link>https://forem.com/_akashkmt_/react-re-render-saga-stopping-the-madness-one-trick-at-a-time-1akg</link>
      <guid>https://forem.com/_akashkmt_/react-re-render-saga-stopping-the-madness-one-trick-at-a-time-1akg</guid>
      <description>&lt;p&gt;Welcome to the &lt;strong&gt;React Re-render Saga&lt;/strong&gt;, where we go full throttle into optimizing your React app and stopping those sneaky re-renders from ruining your vibe. We’ll break it down into bite-sized, meme-worthy posts to help you level up your React game. Let’s keep things smooth and fast, because ain’t nobody got time for laggy apps! 🏃‍♂️💨&lt;/p&gt;




&lt;h2&gt;
  
  
  Post 1: &lt;a href="https://dev.to/_akashkmt_/why-so-many-re-renders-react-3b6b"&gt;Why So Many Re-renders, React? 🤔&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s kick things off by answering the big question: Why does React re-render so much?!&lt;/p&gt;

&lt;p&gt;In this post, we’ll dive into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;props&lt;/code&gt; make React trigger happy.&lt;/li&gt;
&lt;li&gt;Why even a small change can cause React to re-render like it’s got something to prove.&lt;/li&gt;
&lt;li&gt;The classic &lt;code&gt;parent-child&lt;/code&gt; re-render domino effect (seriously, it’s like React thinks we’re building a chain reaction for fun).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end, you'll know exactly what’s causing all these rerenders and be pumped for the solutions that are coming next. Time to flex those React muscles 💪!&lt;/p&gt;




&lt;h2&gt;
  
  
  Post 2: &lt;a href="https://dev.to/_akashkmt_/reactmemo-the-bouncer-for-unnecessary-re-renders-14ke"&gt;🛡️ React.memo: The Bouncer for Unnecessary Re-renders&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In this post, we’re rolling out the big guns. &lt;code&gt;React.memo&lt;/code&gt; is like the bouncer at a club, stopping any unnecessary re-renders at the door.&lt;/p&gt;

&lt;p&gt;We’ll cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How &lt;code&gt;React.memo&lt;/code&gt; puts up the velvet rope so only changed props get through.&lt;/li&gt;
&lt;li&gt;When to use &lt;code&gt;React.memo&lt;/code&gt; like a pro (and when to chill out with it).&lt;/li&gt;
&lt;li&gt;Avoiding common pitfalls, because even &lt;code&gt;React.memo&lt;/code&gt; has its kryptonite.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 3: &lt;a href="https://dev.to/_akashkmt_/usecallback-keeping-your-functions-in-check-literally-51gj"&gt;🔗 useCallback: Keeping Your Functions in Line, Literally&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Functions, right? React loves recreating them like it’s a hobby. useCallback is here to stop that madness and keep your functions in check.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’ll break down why React loves creating fresh functions on every render (like, why tho?).&lt;/li&gt;
&lt;li&gt;How &lt;code&gt;useCallback&lt;/code&gt; keeps your functions from being “new” every time (stability, baby!).&lt;/li&gt;
&lt;li&gt;Real-life scenarios where &lt;code&gt;useCallback&lt;/code&gt; is the MVP of performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 4: 💾 useMemo: Let’s Remember the Important Stuff
&lt;/h2&gt;

&lt;p&gt;Expensive calculations? 😰 You need &lt;code&gt;useMemo&lt;/code&gt;, the ultimate memory keeper! It saves those heavy-lifting operations for when you really need them.&lt;/p&gt;

&lt;p&gt;We’ll dive into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How &lt;code&gt;useMemo&lt;/code&gt; saves you from recalculating the same stuff over and over.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;useMemo&lt;/code&gt; is your best friend (and when you can just wing it).&lt;/li&gt;
&lt;li&gt;Perfect use cases that’ll make your app lightning fast ⚡.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 5: 🕵️‍♂️ useRef: The Sneaky Sidekick That Keeps Quiet
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; is like that friend who’s always there, watching things quietly but never causing any drama. You can store stuff in it, but unlike &lt;code&gt;useState&lt;/code&gt;, it won’t make React freak out and re-render!&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’ll show you when &lt;code&gt;useRef&lt;/code&gt; is better than &lt;code&gt;useState&lt;/code&gt; (hint: when you want to store stuff without re-renders).&lt;/li&gt;
&lt;li&gt;How to use it to keep track of DOM elements or store previous state without causing React panic.&lt;/li&gt;
&lt;li&gt;A bunch of sneaky tricks to make your app perform smoother than ever.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 6: 👶 Combining React.memo &amp;amp; useCallback: The Dynamic Duo
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;React.memo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; team up, it’s like Batman and Robin—together, they’re unstoppable at preventing rerenders! 🦇🦸‍♂️&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’ll show you how to combine these two for ultimate child component efficiency.&lt;/li&gt;
&lt;li&gt;Examples where the parent-child rerender drama finally ends (spoiler: peace restored ✌️).&lt;/li&gt;
&lt;li&gt;How to make sure neither parent nor child re-renders unnecessarily.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 7: 🎟️ Keys in Lists: Give Each Item a VIP Pass
&lt;/h2&gt;

&lt;p&gt;Without unique keys, React's like, “Who’s who?”—and boom! Everything re-renders. Let’s stop that.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn why unique &lt;code&gt;keys&lt;/code&gt; are like VIP passes for your list items 🎫.&lt;/li&gt;
&lt;li&gt;Why not having &lt;code&gt;keys&lt;/code&gt; is like inviting chaos (or re-renders) into your life.&lt;/li&gt;
&lt;li&gt;Best practices to ensure smooth rendering of dynamic lists.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 8: 🚫 Avoid Inline Functions &amp;amp; Objects: Silent Performance Killers
&lt;/h2&gt;

&lt;p&gt;Creating inline functions or objects in your &lt;code&gt;JSX&lt;/code&gt; is a trap! It seems harmless, but it’s like inviting React to re-render everything for no reason. 😵‍💫&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’ll break down why inline functions and objects are bad news for performance.&lt;/li&gt;
&lt;li&gt;How to refactor your code so React doesn’t lose its cool.&lt;/li&gt;
&lt;li&gt;Real-world examples to make sure you’re never caught in the inline trap again.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Post 9: ⏳ Lazy Loading: Why Do Today What You Can Do Tomorrow?
&lt;/h2&gt;

&lt;p&gt;Finally, let’s talk about Lazy Loading. React’s motto here: “Why render the whole thing when you can do it later?” ⌛&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to use &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt; to load components only when you actually need them.&lt;/li&gt;
&lt;li&gt;How lazy loading helps with performance by not overloading your app at once.&lt;/li&gt;
&lt;li&gt;Examples to show how chill React becomes when you don’t make it do everything at once.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎉 Final Words: The React Re-render Saga Awaits!
&lt;/h2&gt;

&lt;p&gt;Over this series, we’re going to unlock all the secrets to stop unnecessary re-renders in their tracks. By the end, you’ll be the master of React performance, zipping through your app without breaking a sweat. Each post will arm you with new React powers—use them wisely!&lt;/p&gt;

&lt;p&gt;Stay tuned for all the React goodness coming your way! ✨ Let’s make React work for us, not against us. Happy optimizing, fellow devs! 🚀&lt;/p&gt;




&lt;h3&gt;
  
  
  Happy Coding :))
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>performance</category>
    </item>
    <item>
      <title>Turbocharging React with useMemo and useCallback: No More Slow Renders! 💨</title>
      <dc:creator>Akash Kumawat</dc:creator>
      <pubDate>Tue, 24 Sep 2024 17:07:52 +0000</pubDate>
      <link>https://forem.com/_akashkmt_/turbocharging-react-with-usememo-and-usecallback-no-more-slow-renders-59be</link>
      <guid>https://forem.com/_akashkmt_/turbocharging-react-with-usememo-and-usecallback-no-more-slow-renders-59be</guid>
      <description>&lt;p&gt;If you're anything like me, you love how smooth React can be... until it isn't! Yep, as your app grows and becomes more complex, you might start noticing some performance slowdowns. But fear not! React gives us a couple of awesome tools &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; to keep things running fast and smooth.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll break down these two hooks in a fun, friendly way, so you can optimize your app without breaking a sweat!&lt;/p&gt;

&lt;h2&gt;
  
  
  🧐 Wait, Why Bother? Isn’t React Fast Enough?
&lt;/h2&gt;

&lt;p&gt;Great question! React is usually blazing fast. But sometimes, if you’re running expensive calculations or passing a lot of functions through your component tree, things can start to drag a bit. Every time your app re-renders (because props or state changed), React has to re-do a lot of stuff.&lt;/p&gt;

&lt;p&gt;That’s where &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; come in. They basically let React “remember” stuff so it doesn’t have to re-do unnecessary work!&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 &lt;code&gt;useMemo&lt;/code&gt;: Making React Remember the Hard Stuff!
&lt;/h2&gt;

&lt;p&gt;Think of &lt;code&gt;useMemo&lt;/code&gt; as a smart assistant. It keeps track of the result of a function and only recalculates it if something important changes. Why bother re-doing a calculation when the inputs haven’t even changed? Exactly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basics:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoizedValue = useMemo(() =&amp;gt; {
  return calculateSomethingExpensive(a, b);
}, [a, b]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;calculateSomethingExpensive&lt;/code&gt; will only run if &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;b&lt;/code&gt; change. Otherwise, React says, "No need to redo that work — I’ve got it saved right here!"&lt;/p&gt;

&lt;h3&gt;
  
  
  A Fun Example:
&lt;/h3&gt;

&lt;p&gt;Let’s say we’re building a component that calculates the total value of a shopping cart. Every time the component re-renders, it recalculates that total. Now, if you’re shopping a lot (which we all do!), this calculation might slow down the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ShoppingCart({ items }) {
  const totalValue = items.reduce((sum, item) =&amp;gt; sum + item.price, 0);

  return &amp;lt;div&amp;gt;Total: {totalValue}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if the &lt;code&gt;items&lt;/code&gt; haven’t changed, we’re still doing the math every time! With &lt;code&gt;useMemo&lt;/code&gt;, we can tell React to only recalculate when the &lt;code&gt;items&lt;/code&gt; array changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ShoppingCart({ items }) {
  const totalValue = useMemo(() =&amp;gt; {
    return items.reduce((sum, item) =&amp;gt; sum + item.price, 0);
  }, [items]);

  return &amp;lt;div&amp;gt;Total: {totalValue}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom! Now your app only recalculates when there’s actually something new in your cart. It’s like React saying, “I’ve got this covered.”&lt;/p&gt;




&lt;h2&gt;
  
  
  🔥 &lt;code&gt;useCallback&lt;/code&gt;: No More New Functions Every Second!
&lt;/h2&gt;

&lt;p&gt;Now let’s talk about &lt;code&gt;useCallback&lt;/code&gt;. If &lt;code&gt;useMemo&lt;/code&gt; is about remembering values, &lt;code&gt;useCallback&lt;/code&gt; is about remembering functions. This is super useful when you’re passing a function as a prop to a child component.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;useCallback&lt;/code&gt;, React recreates your function on every render — this can cause your child components to re-render unnecessarily. And we all know how annoying that can be!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basics:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoizedCallback = useCallback(() =&amp;gt; {
  doSomething(a, b);
}, [a, b]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells React to only recreate the &lt;code&gt;doSomething&lt;/code&gt; function if &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;b&lt;/code&gt; change. If not, React just reuses the previous function!&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Break It Down:
&lt;/h3&gt;

&lt;p&gt;Here’s a common scenario: You have a parent component passing down a &lt;code&gt;handleClick&lt;/code&gt; function to a child component. But every time the parent re-renders, the function gets recreated, forcing the child to re-render too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ParentComponent() {
  const handleClick = () =&amp;gt; {
    console.log('Button clicked!');
  };

  return &amp;lt;ChildComponent onClick={handleClick} /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if nothing has changed, the child component thinks it needs to re-render because the &lt;code&gt;handleClick&lt;/code&gt; function is "new" every time.&lt;/p&gt;

&lt;p&gt;Now, let’s be clever and use &lt;code&gt;useCallback&lt;/code&gt; to avoid this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ParentComponent() {
  const handleClick = useCallback(() =&amp;gt; {
    console.log('Button clicked!');
  }, []);

  return &amp;lt;ChildComponent onClick={handleClick} /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tada! Now the &lt;code&gt;handleClick&lt;/code&gt; function only changes if its dependencies change, and the child component doesn’t re-render unnecessarily.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 &lt;code&gt;useMemo&lt;/code&gt; vs. &lt;code&gt;useCallback&lt;/code&gt;: What’s the Difference? (Spoiler: Both are Awesome!)
&lt;/h2&gt;

&lt;p&gt;Okay, so now you’re probably thinking, “These two sound pretty similar—what’s the difference?”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useMemo&lt;/code&gt;: Caches the result of a function. It’s great when you have some expensive calculation that doesn’t need to be recalculated all the time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useCallback&lt;/code&gt;: Caches the function itself. Perfect when you’re passing functions down as props and want to avoid unnecessary re-renders.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Quick Recap:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;useMemo&lt;/code&gt; to avoid recalculating something unless it’s necessary.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;useCallback&lt;/code&gt; to prevent creating new functions unless their dependencies have changed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚀 When Should You Actually Use These Hooks?
&lt;/h2&gt;

&lt;p&gt;Here’s the golden rule: Don’t overuse &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt;. Seriously! React is already fast enough for most apps, and using these hooks everywhere can add unnecessary complexity.&lt;/p&gt;

&lt;p&gt;When to Use &lt;code&gt;useMemo&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re doing something heavy, like filtering or sorting a large list, or performing complex math.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use &lt;code&gt;useCallback&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re passing functions down to child components, and you notice those components are re-rendering more than they need to (especially if they’re wrapped in &lt;code&gt;React.memo&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Measure performance before and after using these hooks. If you notice a speed boost, great! If not, maybe it’s not worth the extra complexity.&lt;/p&gt;




&lt;h2&gt;
  
  
  😱 Pitfalls: Don’t Be That Person!
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Overuse: Please, don’t slap &lt;code&gt;useMemo&lt;/code&gt; or &lt;code&gt;useCallback&lt;/code&gt; on every single function or calculation. It can actually hurt performance if used excessively. Only memoize when you need to.&lt;/li&gt;
&lt;li&gt;Wrong Dependencies: If you forget to include the right dependencies in your dependency array, your memoized function might not update correctly. Always double-check what variables your function depends on.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎉 Wrapping Up: Keep it Fun and Fast!
&lt;/h2&gt;

&lt;p&gt;So there you have it! &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; are awesome tools to keep your React app snappy, but remember—React is already super fast. Use these hooks when you need to optimize performance, but don’t go overboard. Simplicity is key!&lt;/p&gt;

&lt;p&gt;With that, go out there and make your app lightning-fast (and have fun while doing it)&lt;/p&gt;




&lt;h3&gt;
  
  
  Happy Coding :))
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>performance</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
