<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>react optimization Archives - Azad Chouhan</title>
	<atom:link href="https://azadchouhan.online/tag/react-optimization/feed/" rel="self" type="application/rss+xml" />
	<link>https://azadchouhan.online/tag/react-optimization/</link>
	<description>Web Developer &#38; Digital Marketing Expert in WordPress, React, PHP &#38; Shopify</description>
	<lastBuildDate>Mon, 22 Sep 2025 12:35:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://azadchouhan.online/wp-content/uploads/2025/08/cropped-azad-chouhan-32x32.png</url>
	<title>react optimization Archives - Azad Chouhan</title>
	<link>https://azadchouhan.online/tag/react-optimization/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>useMemo vs useCallback in React: Simple Explanation with Examples</title>
		<link>https://azadchouhan.online/react/usememo-vs-usecallback-in-react-simple-explanation-with-examples/</link>
					<comments>https://azadchouhan.online/react/usememo-vs-usecallback-in-react-simple-explanation-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[azad chouhan]]></dc:creator>
		<pubDate>Mon, 22 Sep 2025 12:35:16 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[React hooks]]></category>
		<category><![CDATA[react memoization]]></category>
		<category><![CDATA[react optimization]]></category>
		<category><![CDATA[React performance optimization]]></category>
		<category><![CDATA[useCallback example]]></category>
		<category><![CDATA[useMemo example]]></category>
		<category><![CDATA[useMemo vs useCallback]]></category>
		<guid isPermaLink="false">https://azadchouhan.online/?p=1032</guid>

					<description><![CDATA[<p>Learn the key differences between useMemo and useCallback in React with simple examples. Boost your app performance by using these hooks the right way</p>
<p>The post <a href="https://azadchouhan.online/react/usememo-vs-usecallback-in-react-simple-explanation-with-examples/">useMemo vs useCallback in React: Simple Explanation with Examples</a> appeared first on <a href="https://azadchouhan.online">Azad Chouhan</a>.</p>
]]></description>
										<content:encoded><![CDATA[<article>
<header>
<h2>useMemo vs useCallback in React: Key Differences Explained for Beginners</h2>
</header>
<p><strong>useMemo vs useCallback</strong> : When you start working with <strong>React</strong>, you often hear about <strong><code>useMemo()</code></strong> and <strong><code>useCallback()</code></strong>. At first, they may seem confusing because both are used to <strong>optimize performance</strong>. I remember when I was learning React, I kept mixing them up. If you’ve been there too, don’t worry — in this guide, I’ll explain their differences in <strong>simple words</strong>, so you’ll never forget them again.</p>
<h2>Why Performance Optimization Matters in React</h2>
<p>React re-renders components whenever their state or props change. While this is good for keeping the UI updated,<br />
it can sometimes <strong>slow down your app</strong> if there’s heavy computation or unnecessary re-renders.</p>
<p>That’s where <strong>useMemo</strong> and <strong>useCallback</strong> come to the rescue.<br />
They help <strong>skip unnecessary calculations</strong> and<br />
<strong>prevent components from re-rendering</strong> when it’s not needed.</p>
<h2>What is <code>useMemo()</code> in React?</h2>
<p>The <strong><code>useMemo()</code></strong> hook is used to <strong>memoize</strong> the<br />
<strong>result of a function</strong>. This means React will<br />
<strong>store the computed value</strong> and only re-run the function if the <strong>dependencies</strong> change.</p>
<h3>Simple Example of useMemo:</h3>
<pre><code>
import React, { useMemo, useState } from "react";

function App() {
  const [count, setCount] = useState(0);
  const [text, setText] = useState("");

  const expensiveCalculation = useMemo(() =&gt; {
    console.log("Calculating...");
    return count * 2;
  }, [count]);

  return (
    &lt;div&gt;
      &lt;h1&gt;Result: {expensiveCalculation}&lt;/h1&gt;
      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Increase&lt;/button&gt;
      &lt;input value={text} onChange={(e) =&gt; setText(e.target.value)} /&gt;
    &lt;/div&gt;
  );
}
    </code></pre>
<p><strong>How it works:</strong></p>
<ul>
<li>The expensive calculation <strong>only runs when <code>count</code> changes</strong>.</li>
<li>Changing the <code>text</code> will <strong>not trigger the calculation</strong>, saving performance.</li>
</ul>
<h2>What is <code>useCallback()</code> in React?</h2>
<p>The <strong><code>useCallback()</code></strong> hook is used to <strong>memoize a function itself</strong>,<br />
not its result. This is useful when passing functions as props to child components because it prevents them from<br />
<strong>unnecessary re-rendering</strong>.</p>
<h3>Simple Example of useCallback:</h3>
<pre><code>
import React, { useState, useCallback } from "react";

function Button({ onClick }) {
  console.log("Button rendered!");
  return &lt;button onClick={onClick}&gt;Click Me&lt;/button&gt;;
}

function App() {
  const [count, setCount] = useState(0);

  const handleClick = useCallback(() =&gt; {
    setCount((prev) =&gt; prev + 1);
  }, []);

  return (
    &lt;div&gt;
      &lt;h1&gt;Count: {count}&lt;/h1&gt;
      &lt;Button onClick={handleClick} /&gt;
    &lt;/div&gt;
  );
}
    </code></pre>
<p><strong>How it works:</strong></p>
<ul>
<li>The <code>handleClick</code> function <strong>stays the same between renders</strong>.</li>
<li>The <code>&lt;Button /&gt;</code> component will <strong>not re-render</strong> unless required, improving performance.</li>
</ul>
<h2>Key Differences Between useMemo and useCallback</h2>
<table border="1" cellspacing="0" cellpadding="8">
<thead>
<tr>
<th>Feature</th>
<th>useMemo()</th>
<th>useCallback()</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Purpose</strong></td>
<td>Memoizes <strong>computed value</strong></td>
<td>Memoizes <strong>function itself</strong></td>
</tr>
<tr>
<td><strong>Returns</strong></td>
<td>The <strong>result</strong> of the calculation</td>
<td>The <strong>function</strong> itself</td>
</tr>
<tr>
<td><strong>Used for</strong></td>
<td>Expensive calculations or derived data</td>
<td>Passing stable functions to child components</td>
</tr>
<tr>
<td><strong>Example use case</strong></td>
<td>Calculating total price of items</td>
<td>Optimizing event handlers in child components</td>
</tr>
</tbody>
</table>
<h2>When to Use useMemo and useCallback</h2>
<ul>
<li><strong>Use <code>useMemo()</code>:</strong> When you have a <strong>heavy calculation</strong> or<br />
<strong>derived data</strong> that should not re-run unnecessarily.</li>
<li><strong>Use <code>useCallback()</code>:</strong> When you pass functions as<br />
<strong>props to child components</strong>, especially if those children are wrapped in <code>React.memo()</code>.</li>
</ul>
<h2>Best Practices</h2>
<ul>
<li><strong>Don’t overuse these hooks</strong> – unnecessary usage can make code harder to read.</li>
<li>Use them <strong>only for performance bottlenecks</strong>.</li>
<li>Always <strong>provide correct dependency arrays</strong>, or you might run into bugs.</li>
</ul>
<h2>Final Thoughts</h2>
<p>When I first started using React, I would randomly add <code>useMemo</code> and <code>useCallback</code> everywhere,<br />
thinking they would magically speed up my app. Over time, I learned that<br />
<strong>understanding the problem first</strong> is the key.</p>
<p>&#8211; If you need to <strong>cache a value</strong>, use <code>useMemo()</code>.<br />
&#8211; If you need to <strong>cache a function</strong>, use <code>useCallback()</code>.</p>
<p>Both are powerful tools for improving <strong>React app performance</strong>, but only when used wisely.</p>
<footer><span style="font-size: 13.3333px;"> </span></footer>
</article>
<p>The post <a href="https://azadchouhan.online/react/usememo-vs-usecallback-in-react-simple-explanation-with-examples/">useMemo vs useCallback in React: Simple Explanation with Examples</a> appeared first on <a href="https://azadchouhan.online">Azad Chouhan</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://azadchouhan.online/react/usememo-vs-usecallback-in-react-simple-explanation-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
