<?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: avinashmahlawat</title>
    <description>The latest articles on Forem by avinashmahlawat (@avinashmahlawat).</description>
    <link>https://forem.com/avinashmahlawat</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%2F3485137%2F0d351c64-aa83-435a-919f-04abbb4c440d.jpeg</url>
      <title>Forem: avinashmahlawat</title>
      <link>https://forem.com/avinashmahlawat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/avinashmahlawat"/>
    <language>en</language>
    <item>
      <title>"useMemoization" in reactjs</title>
      <dc:creator>avinashmahlawat</dc:creator>
      <pubDate>Sun, 07 Sep 2025 18:55:35 +0000</pubDate>
      <link>https://forem.com/avinashmahlawat/usememoization-in-reactjs-286n</link>
      <guid>https://forem.com/avinashmahlawat/usememoization-in-reactjs-286n</guid>
      <description>&lt;p&gt;Memoization in React is an optimization technique used to prevent unnecessary re-renders of components or re-calculations of expensive values. It works by caching the result of a function or component based on its inputs (props or dependencies) and returning the cached result if the inputs haven't changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Memoizing Components with React.memo:&lt;/strong&gt;&lt;br&gt;
React.memo is a higher-order component (HOC) used to memoize functional components. It prevents the component from re-rendering if its props have not changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

const MyMemoizedComponent = React.memo(({ prop1, prop2 }) =&amp;gt; {
  // This component will only re-render if prop1 or prop2 change
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Prop 1: {prop1}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Prop 2: {prop2}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Memoizing Values with useMemo:&lt;/strong&gt;&lt;br&gt;
The useMemo hook is used to memoize the result of an expensive calculation. The calculation will only be re-executed if its dependencies change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useMemo } from 'react';

function MyComponent({ data }) {
  const expensiveValue = useMemo(() =&amp;gt; {
    // Perform an expensive calculation using 'data'
    return data.map(item =&amp;gt; item * 2); 
  }, [data]); // Re-calculate only when 'data' changes

  return (
    &amp;lt;div&amp;gt;
      {/* Render expensiveValue */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Memoizing Callbacks with useCallback:&lt;/strong&gt;&lt;br&gt;
The useCallback hook is used to memoize a function, preventing it from being re-created on every render. This is particularly useful when passing callbacks as props to memoized child components to avoid unnecessary re-renders of the child.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useCallback } from 'react';

function ParentComponent() {
  const handleClick = useCallback(() =&amp;gt; {
    console.log('Button clicked!');
  }, []); // The function will not be re-created unless dependencies change

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

const ChildComponent = React.memo(({ onClick }) =&amp;gt; {
  return &amp;lt;button onClick={onClick}&amp;gt;Click Me&amp;lt;/button&amp;gt;;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to Use Memoization:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Expensive calculations:&lt;/strong&gt;&lt;br&gt;
Memoize the results of computations that are time-consuming or resource-intensive.&lt;br&gt;
&lt;strong&gt;Preventing unnecessary re-renders:&lt;/strong&gt;&lt;br&gt;
Use React.memo for components that receive stable props and whose re-renders are costly.&lt;br&gt;
&lt;strong&gt;Optimizing callback functions:&lt;/strong&gt;&lt;br&gt;
Use useCallback when passing functions as props to memoized child components.&lt;/p&gt;

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