<?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: Neeraj Jangir</title>
    <description>The latest articles on Forem by Neeraj Jangir (@njneeraj).</description>
    <link>https://forem.com/njneeraj</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%2F1662368%2F9e038b9c-6307-4bd6-985e-dafaf324fc8c.jpeg</url>
      <title>Forem: Neeraj Jangir</title>
      <link>https://forem.com/njneeraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/njneeraj"/>
    <language>en</language>
    <item>
      <title>React hooks deep dive - useCallback, useMemo, and memo</title>
      <dc:creator>Neeraj Jangir</dc:creator>
      <pubDate>Tue, 30 Jul 2024 16:57:13 +0000</pubDate>
      <link>https://forem.com/njneeraj/react-hooks-deep-dive-usecallback-usememo-and-memo-4703</link>
      <guid>https://forem.com/njneeraj/react-hooks-deep-dive-usecallback-usememo-and-memo-4703</guid>
      <description>&lt;h2&gt;
  
  
  React hooks deep dive - useCallback, useMemo, and memo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6t5aiz45ohzyle4b6lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6t5aiz45ohzyle4b6lw.png" alt="Image react hooks useCallback,useMemo and memo hooks" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the ever-evolving world of React, mastering hooks is essential for writing efficient and optimized code. But wait, what exactly are hooks? In the simplest terms, hooks are like real-life hooks that help us attach or "hook" into certain things within our React components.&lt;br&gt;
In context of React, Hooks are essentially functions that keep some data consistent across these re-renders.&lt;/p&gt;

&lt;p&gt;When we talk about React components, they re-render whenever their state or props change.Re-rendering is crucial for good user experience and interactivity. But why does this happen? It often happens because JavaScript treats every function as a new object, causing reference changes even when the function logic remains the same.&lt;/p&gt;

&lt;p&gt;The next section is crucial, pay attention&lt;/p&gt;
&lt;h3&gt;
  
  
  Understanding Primitive Values vs. Objects in JavaScript
&lt;/h3&gt;

&lt;p&gt;In JavaScript, we have two categories of data: primitive values (like numbers and strings) and objects (including functions, arrays, classes, etc.). Primitive values are compared by their value, while objects are compared by their reference or memory address.&lt;/p&gt;

&lt;p&gt;When a React component re-renders, it compares the previous and current props to determine if it should update the component. For primitive values, this comparison is straightforward because they are compared by value. For example, if a number was 5 in the previous render and is still 5 in the current render, React recognizes no change and doesn't re-render the component.&lt;/p&gt;

&lt;p&gt;However, functions are objects in JavaScript and are compared by reference. Each time a component re-renders, the function gets a new memory address, even if the function logic remains the same. As a result, React considers it a new function and will re-render the child component that receives this function as a prop, which can degrade performance.&lt;/p&gt;
&lt;h3&gt;
  
  
  Introducing useCallback, useMemo, and memo
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;useCallback&lt;/strong&gt; is a hook that helps memoize a function so that it doesn't get re-created on every re-render until its dependencies change. This is crucial for optimizing performance, especially in components that rely heavily on callback functions passed down as props.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;strong&gt;useMemo&lt;/strong&gt; is used to memoize a value or object. Imagine you have an e-commerce application, and on the frontend, you receive a large object of data. From this, you calculate the average of likes to show the rating. Since the value is calculated from the object, each re-render assigns it a new memory address, which we want to avoid. Just like &lt;code&gt;useCallback&lt;/code&gt;, we use &lt;code&gt;useMemo&lt;/code&gt; to memoize that value.&lt;/p&gt;

&lt;p&gt;And when we need to memoize an entire component, we wrap the component in &lt;strong&gt;memo&lt;/strong&gt;. Since every component is essentially a function that returns JSX, memoizing it can prevent unnecessary re-renders.&lt;/p&gt;
&lt;h3&gt;
  
  
  Practical Usage
&lt;/h3&gt;

&lt;p&gt;Now, let's see how to use these hooks.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;useCallback&lt;/code&gt; is straightforward. It takes two arguments: the function to memoize and an array of dependencies. The function will only be re-created if one of the dependencies changes.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// useCallback example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ShowCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CountButton&lt;/span&gt; &lt;span class="na"&gt;onIncrement&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// useMemo example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;calculateRating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalLikes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;totalLikes&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;totalLikes&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;RatingComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;calculateRating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Average Rating: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;rating&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// memo example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CountButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;onIncrement&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onIncrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Increment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;useCallback&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt;, and &lt;code&gt;memo&lt;/code&gt;, you can significantly improve the performance of your React applications, avoiding unnecessary re-renders and ensuring smoother user experiences.&lt;/p&gt;

&lt;p&gt;I hope this helped you get a better Understanding of these hooks.At the end these are all JavaScript functions.&lt;br&gt;
Happy coding!.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>usecallback</category>
      <category>usememo</category>
    </item>
  </channel>
</rss>
