{"id":178,"date":"2026-10-22T03:37:38","date_gmt":"2026-10-22T03:37:38","guid":{"rendered":"https:\/\/webdevpuneet.com\/blog\/?p=178"},"modified":"2026-10-22T03:37:42","modified_gmt":"2026-10-22T03:37:42","slug":"usememo-vs-usecallback-what-they-actually-do-8-re-render-demos","status":"publish","type":"post","link":"https:\/\/webdevpuneet.com\/blog\/usememo-vs-usecallback-what-they-actually-do-8-re-render-demos\/","title":{"rendered":"useMemo vs useCallback: What They Actually Do (8 Re-Render Demos)"},"content":{"rendered":"<p><!--\n================================================================\n  POST TITLE (paste into the WordPress title field \u2014 it becomes the page H1)\n  useMemo vs useCallback: What They Actually Do (8 Re-Render Demos)\n\n  SEO TITLE (55 chars, target 50\u201360, max 60 \u2014 \"Optimize SEO\" panel \u2192 SEO TITLE)\n  useMemo vs useCallback: What They Actually Do (8 Demos)\n\n  SEO DESCRIPTION (157 chars, target 150\u2013160, max 160 \u2014 \"Optimize SEO\" panel \u2192 SEO DESCRIPTION)\n  useMemo vs useCallback explained with 8 live re-render demos: React.memo, stale dependencies, premature memoization, effect loops and why memo isn't a cache.\n\n  HOW TO PASTE: click the first line after this comment, press Ctrl+Shift+End, Ctrl+C.\n  In WordPress click \"Type \/ to choose a block\", then Ctrl+V.\n  Fallback: \u22ee menu \u2192 Code editor \u2192 paste.\n================================================================\n--><\/p>\n\n\n<p class=\"wp-block-paragraph\">Most explanations of <code>useMemo<\/code> and <code>useCallback<\/code> stop at &#8220;one caches a value, the other caches a function,&#8221; which is true and also not nearly enough to use them correctly. The interesting part is <em>why<\/em> that distinction matters \u2014 how React actually compares dependencies, what &#8220;referential equality&#8221; means for an object literal versus a number, and why wrapping something in either hook can make performance worse instead of better if you reach for it out of habit. Both hooks are thin wrappers around the same idea, and most of the bugs people blame on them are really just React.memo&#8217;s shallow comparison, or <code>Object.is<\/code>, doing exactly what they were always going to do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The 8 demos below are all live and interactive, and every one that has a &#8220;before\/after&#8221; split keeps both sides running next to each other so you can watch the actual difference in render counts, not just read about it. Every embed also has a <strong>Fork &amp; Edit<\/strong> button that opens the exact snippet, already running, in <a href=\"https:\/\/webdevpuneet.com\/ui-snippets\/mycode\/\">My Code<\/a> \u2014 webdevpuneet&#8217;s free in-browser editor. If you&#8217;d rather build one of these from a blank slate, the <a href=\"https:\/\/webdevpuneet.com\/react-playground\/\">React Playground<\/a> is webdevpuneet&#8217;s full in-browser React sandbox, no install or build step required for either.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The one-sentence rule<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>var total = useMemo(function () {\n  return expensiveCalc(a, b);   \/\/ only re-runs when a or b actually changed\n}, &#91;a, b]);\n\nvar handleClick = useCallback(function () {\n  doSomething(a);               \/\/ same function REFERENCE across renders\n}, &#91;a]);                        \/\/ as long as `a` hasn't changed<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useMemo(fn, deps)<\/code> calls <code>fn<\/code> during render and caches its return value; on the next render, if every entry in <code>deps<\/code> is still <code>Object.is<\/code>-equal to last time, React hands back the cached value instead of calling <code>fn<\/code> again. <code>useCallback(fn, deps)<\/code> is the exact same mechanism applied to the function itself \u2014 it&#8217;s <code>useMemo(function () { return fn; }, deps)<\/code>, nothing more. Neither hook makes anything happen that wouldn&#8217;t happen anyway; both are pure &#8220;skip the work if nothing relevant changed&#8221; checks, and that check has its own small cost. The 8 demos below cover the ways that check pays off, the ways it silently does nothing, and the ways getting the dependency array wrong quietly breaks correctness rather than just performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. useMemo Skips a Recalculation vs Redoing It Every Render<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click &#8220;Unrelated tick&#8221; a few times first, then click &#8220;Add 500 items.&#8221; Watch the calculation counter on each side.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/1.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"useMemo Skips a Recalculation vs Redoing It Every Render Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ BROKEN: recalculates on every render, tick included\nvar sum = expensiveSum(items);\n\n\/\/ FIXED: only recalculates when `items` itself changes\nvar sum = useMemo(function () {\n  return expensiveSum(items);\n}, &#91;items]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without <code>useMemo<\/code>, the calculation is just a line of code in the render body, and React re-runs the entire component function on every render regardless of what triggered it \u2014 an unrelated state update, a parent re-rendering, anything. That means an expensive calculation pays its full cost every single time, even when its actual inputs are unchanged. <code>useMemo<\/code> intercepts that: React keeps the last computed value and the dependency array it was computed from, and on each subsequent render it compares the new dependency array to the old one entry-by-entry with <code>Object.is<\/code>. If nothing differs, the cached value is returned and the function passed to <code>useMemo<\/code> never runs at all \u2014 not even to recompute the same answer. In the demo, clicking the unrelated tick button re-renders the component, but the item list hasn&#8217;t changed, so the memoized side&#8217;s calculation counter simply doesn&#8217;t move; the unmemoized side&#8217;s counter climbs on every click regardless.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> calculations that are measurably expensive (sorting or filtering large arrays, heavy math, building derived data structures) and that don&#8217;t need to change on every render. <strong>Tip:<\/strong> &#8220;expensive&#8221; is relative to your actual frame budget \u2014 profile before reaching for this, because the dependency-array check itself isn&#8217;t free, as demo #4 below shows directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. A Plain Function Prop Breaks React.memo<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click &#8220;Unrelated tick&#8221; a few times. Neither child&#8217;s actual content changes \u2014 watch which one re-renders anyway.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/2.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"A Plain Function Prop Breaks React.memo Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ BROKEN: a new function reference every render\nvar Child = React.memo(ChildImpl);\nfunction handleClick() { doThing(); }\ne(Child, { onAction: handleClick });\n\n\/\/ FIXED: same function reference across renders\nvar handleClick = useCallback(function () { doThing(); }, &#91;]);\ne(Child, { onAction: handleClick });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>React.memo<\/code> wraps a component so React can skip re-rendering it when its props are shallowly equal to last time \u2014 each prop compared individually with <code>Object.is<\/code>, the same comparison the dependency array uses. A function declared inside a component body is a brand-new object on every render, even if its code is byte-for-byte identical to the previous one; two functions are never <code>===<\/code> to each other just because they do the same thing. Pass that fresh function as a prop, and <code>React.memo<\/code>&#8216;s shallow compare sees a prop that &#8220;changed,&#8221; so it renders the child anyway \u2014 the memoization is defeated by a single unstable prop, regardless of how many other props are perfectly stable. <code>useCallback<\/code> fixes this the same way <code>useMemo<\/code> fixes an unstable object: it hands back the identical function reference across renders where the dependency array hasn&#8217;t changed, so the shallow compare actually finds equality and <code>React.memo<\/code> gets to do its job.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any function passed as a prop to a component wrapped in <code>React.memo<\/code> \u2014 event handlers, callbacks, render props. <strong>Tip:<\/strong> <code>useCallback<\/code> alone accomplishes nothing if the receiving component isn&#8217;t wrapped in <code>React.memo<\/code> (or doesn&#8217;t otherwise rely on reference equality) \u2014 it stabilizes a reference, but nothing is checking that reference unless something downstream cares.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. A Missing Dependency Serves a Stale Memoized Value<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click &#8220;+1 qty&#8221; a few times without touching price. Watch the total on each side \u2014 one of them stops updating.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/3.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"A Missing Dependency Serves a Stale Memoized Value Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ BROKEN: reads qty, but qty isn't in the dependency array\nvar total = useMemo(function () {\n  return price * qty;\n}, &#91;price]);\n\n\/\/ FIXED: every value the calculation reads is listed\nvar total = useMemo(function () {\n  return price * qty;\n}, &#91;price, qty]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useMemo<\/code> has no way to inspect what a function actually reads \u2014 it only knows to compare the dependency array you hand it. If the function body reads <code>qty<\/code> but <code>qty<\/code> isn&#8217;t in that array, React has no idea that value matters, and it keeps returning the cached result from the last time an array entry it does know about (<code>price<\/code>) changed. This is functionally identical to <code>useEffect<\/code>&#8216;s missing-dependency bug \u2014 a stale closure \u2014 except instead of an effect doing the wrong thing at the wrong time, it&#8217;s a value silently drifting out of sync with the UI that&#8217;s showing it, without any error, warning, or crash. In the demo, incrementing quantity does nothing to the broken total until price is also touched, at which point the memo recalculates and briefly looks &#8220;caught up&#8221; using whatever quantity happens to be current at that moment \u2014 which is correct by coincidence, not by correctness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> catching a calculation that silently reads more state than its dependency array admits to \u2014 this is exactly what the ESLint <code>exhaustive-deps<\/code> rule checks for, and it applies to <code>useMemo<\/code> and <code>useCallback<\/code> identically to <code>useEffect<\/code>. <strong>Tip:<\/strong> if you find yourself wanting to leave a value out of the array on purpose &#8220;because it doesn&#8217;t need to trigger a recompute,&#8221; that&#8217;s usually a sign the value belongs in a ref instead, not that the array should lie about it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Memoizing a Cheap Calculation Buys You Nothing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Type in the box. The dependency changes on every keystroke, so both sides recalculate the same number of times.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/4.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"Memoizing a Cheap Calculation Buys You Nothing Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ No useMemo -- just call it\nvar shouted = shout(text);\n\n\/\/ useMemo'd -- but `text` changes on every keystroke,\n\/\/ so the dependency check never has anything to skip\nvar shouted = useMemo(function () { return shout(text); }, &#91;text]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useMemo<\/code> only saves work when the dependency array is actually stable across some renders \u2014 when its comparison genuinely finds a match some of the time. If the calculation&#8217;s only dependency changes on essentially every render (a text input bound to every keystroke is the clearest case), the comparison never succeeds, the cached branch never gets taken, and the calculation runs exactly as often as it would have without <code>useMemo<\/code> at all. Worse, it&#8217;s not actually free: React still has to allocate space to store the previous dependency array and result, compare each array entry with <code>Object.is<\/code> on every render, and hold onto that memory for the component&#8217;s lifetime. For a calculation as cheap as an uppercase transform, that bookkeeping is very likely more expensive than the calculation it&#8217;s guarding \u2014 the memoization overhead exceeds the cost it was meant to avoid, which is the textbook shape of a premature optimization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> nothing, deliberately \u2014 this demo exists to show the failure mode of wrapping everything in <code>useMemo<\/code> reflexively. <strong>Tip:<\/strong> ask two questions before adding it: is the calculation itself actually expensive, and does its dependency array stay the same across a meaningful number of renders? If either answer is no, skip it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. useCallback Stabilizes a Function Inside useEffect&#8217;s Deps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click Start on each side and watch the effect-run count. One climbs on its own; the other runs once and stops.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/5.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"useCallback Stabilizes a Function Inside useEffect's Deps Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ BROKEN: fetchData is new every render, so this dep is always \"different\"\nfunction fetchData() { return getSomething(); }\nuseEffect(function () { fetchData(); }, &#91;fetchData]);\n\n\/\/ FIXED: same reference, so the effect only re-runs when it should\nvar fetchData = useCallback(function () { return getSomething(); }, &#91;]);\nuseEffect(function () { fetchData(); }, &#91;fetchData]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the useEffect classic \u2014 an infinite update loop \u2014 arriving from the function side instead of the object side. A function declared fresh inside a component is a new reference every render, exactly like an inline object literal; putting it directly into an effect&#8217;s dependency array means the effect sees a &#8220;changed&#8221; dependency on every render it causes, and if that effect also updates state (directly, or via whatever <code>fetchData<\/code> does), the loop is complete: render creates a new function, effect sees it as different and re-runs, re-running updates state, updating state triggers a render, which creates another new function. <code>useCallback<\/code> breaks the cycle the same way <code>useMemo<\/code> breaks it for an object dependency \u2014 by returning the identical function reference across renders where its own dependencies haven&#8217;t changed, so the effect&#8217;s comparison finally finds a match and stops re-running. This demo caps the broken side at 40 runs so Forking it and removing <code>useCallback<\/code> can&#8217;t freeze your tab.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any function defined inside a component that also appears in another hook&#8217;s dependency array \u2014 <code>useEffect<\/code>, another <code>useMemo<\/code>, or a custom hook. <strong>Tip:<\/strong> this is the functional twin of demo #6 below \u2014 same underlying cause, same fix shape, different hook holding the reference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. An Unmemoized Object Wrecks a Downstream useMemo<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click &#8220;Unrelated tick&#8221; a few times, then try &#8220;Raise min price.&#8221; Watch the filter-run count on each side.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/6.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"An Unmemoized Object Wrecks a Downstream useMemo Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Parent hands the child a new object every render\nvar options = { min: minPrice };\n\n\/\/ Child does its own memoized work, keyed on that object\nvar filtered = useMemo(function () {\n  return items.filter(function (n) { return n &gt;= options.min; });\n}, &#91;options]);   \/\/ never stable unless the PARENT memoized `options`<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Memoization composes across component boundaries, which means an unmemoized value at the top can defeat a correctly-written <code>useMemo<\/code> several components downstream. The child in this demo does everything right \u2014 it memoizes its own filtering work, keyed on the <code>options<\/code> object it receives as a prop. But if the parent builds that object as a fresh literal on every render, the child&#8217;s dependency array is comparing a new object to a new object every time, and the comparison can never succeed, no matter how carefully the child itself is written. The fix has to happen at the source: the parent memoizing <code>options<\/code> with its own <code>useMemo<\/code>, keyed on the actual primitive value (<code>minPrice<\/code>) that determines its contents, is what gives the child something stable to compare against. This is also exactly why passing raw object or array literals as props to a memoized child is worth a second look, even when the child looks correctly optimized on its own.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> any object, array, or function that&#8217;s built in a parent and handed to a child that memoizes work based on it \u2014 the stability has to be established where the value is created, not where it&#8217;s consumed. <strong>Tip:<\/strong> if you&#8217;re debugging a memo that &#8220;should&#8221; be skipping work but isn&#8217;t, check one level up the component tree before assuming the memo itself is wrong.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. useMemo Caches a Value, useCallback Only Caches a Function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click &#8220;Unrelated tick&#8221; a few times. Both sides display the same greeting \u2014 watch the compute counter on each.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/7.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"useMemo Caches a Value, useCallback Only Caches a Function Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ useMemo -- caches the RESULT of calling the function\nvar greeting = useMemo(function () { return buildGreeting(name); }, &#91;name]);\n\n\/\/ useCallback -- caches the FUNCTION, not its result\nvar getGreeting = useCallback(function () { return buildGreeting(name); }, &#91;name]);\nvar greeting = getGreeting();   \/\/ still recomputes on every render this runs<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because <code>useCallback(fn, deps)<\/code> is really just <code>useMemo(function () { return fn; }, deps)<\/code>, what it caches is the function object itself, not whatever that function would return if you called it. Calling <code>getGreeting()<\/code> executes the function body immediately, every time you call it \u2014 the stable reference has nothing to do with how many times the body has run, only with whether the reference handed to whoever&#8217;s holding it changed. <code>useMemo<\/code>, by contrast, calls the function itself internally and caches the return value, so the caller never re-executes anything on a render where the dependency array matched. Reaching for <code>useCallback<\/code> to &#8220;cache a value&#8221; is a common mix-up specifically because both hooks look nearly identical at the call site \u2014 the difference only becomes visible once you notice one of them requires an extra pair of parentheses to actually use, and that those parentheses re-run the whole calculation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> making the useMemo\/useCallback distinction concrete rather than definitional \u2014 reach for <code>useMemo<\/code> when you want a cached value, <code>useCallback<\/code> when you want a cached function reference to hand to something else (an event handler, a dependency array, a memoized child). <strong>Tip:<\/strong> if you&#8217;re about to call the thing <code>useCallback<\/code> gave you and use its return value directly, you almost certainly wanted <code>useMemo<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. useMemo Only Remembers Last Render, Not History<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Click 3, then 7, then 3 again. Watch the recompute count \u2014 one side recomputes for the repeat, the other doesn&#8217;t.<\/p>\n\n\n\n<iframe src=\"https:\/\/webdevpuneet.com\/demos\/a1\/18\/8.html\" style=\"width:100%;height:480px;border:0;border-radius:10px;overflow:hidden;\" loading=\"lazy\" title=\"useMemo Only Remembers Last Render, Not History Demo by webdevpuneet.com\"><\/iframe>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The rule:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ useMemo only compares against the PREVIOUS render's deps -- one slot\nvar result = useMemo(function () { return slowSquare(n); }, &#91;n]);\n\n\/\/ A real cache remembers every key it has ever seen\nif (!(n in cacheRef.current)) cacheRef.current&#91;n] = slowSquare(n);\nvar result = cacheRef.current&#91;n];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useMemo<\/code> is often described loosely as &#8220;caching,&#8221; which invites the assumption that once a value has been computed for a given input, it stays cached for that input forever. It doesn&#8217;t \u2014 React holds exactly one cached result at a time, tied to exactly one previous dependency array, and the comparison is only ever against the immediately preceding render. Set <code>n<\/code> to 3, then 7, then back to 3: the third render&#8217;s dependency array (<code>[3]<\/code>) is compared only against the second render&#8217;s (<code>[7]<\/code>), which differs, so the function runs again \u2014 even though 3 was computed just one step earlier. A genuine cache, keyed by every value it has ever seen (the ref-based object on the right), remembers that earlier result and returns it without recomputation. This is the clearest possible proof that <code>useMemo<\/code> is a last-render optimization, not a memoization table \u2014 the word &#8220;memo&#8221; describes the mechanism, not a promise about how long anything is remembered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Best for:<\/strong> correcting the mental model before it causes a real bug \u2014 code that assumes a memoized value is safe to treat as permanently cached (for cache invalidation, deduplication, or &#8220;compute once&#8221; logic) will be wrong the moment inputs oscillate. <strong>Tip:<\/strong> if you actually need a value cached across many distinct inputs over a component&#8217;s lifetime, that&#8217;s a ref holding a real lookup structure (as in the fixed side here), not <code>useMemo<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common pitfalls<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Assuming either hook prevents a calculation from running instead of just sometimes skipping it.<\/strong> Demos #1 and #4 are the same mechanism with opposite outcomes \u2014 it only pays off when the dependency array is actually stable across renders.<\/li>\n\n\n\n<li><strong>Wrapping a callback in useCallback without wrapping the component that receives it in React.memo.<\/strong> Demo #2&#8217;s fix does nothing if nothing downstream is checking reference equality in the first place.<\/li>\n\n\n\n<li><strong>Leaving a value out of the dependency array because &#8220;it doesn&#8217;t need to trigger a recompute.&#8221;<\/strong> Demo #3 shows the actual result \u2014 not a missed update, but a wrong one that looks correct by coincidence.<\/li>\n\n\n\n<li><strong>Memoizing a value one level too late in the component tree.<\/strong> Demo #6 shows a correctly-written child memo defeated entirely by an unmemoized object one level up.<\/li>\n\n\n\n<li><strong>Treating useMemo&#8217;s cached value as a permanent, key-based cache.<\/strong> Demo #8 is the direct proof \u2014 it only ever remembers the immediately previous render, nothing further back.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Should I wrap every function I pass as a prop in useCallback?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No \u2014 only when the function is passed to something that actually checks reference equality, most commonly a child wrapped in <code>React.memo<\/code> or another hook&#8217;s dependency array (as in demos #2 and #5). If the receiving component isn&#8217;t memoized and nothing downstream compares the reference, <code>useCallback<\/code> adds its own small overhead for zero benefit, the same overengineering demo #4 shows for <code>useMemo<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does useMemo guarantee the calculation won&#8217;t run twice?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The React docs are explicit that <code>useMemo<\/code> is a performance optimization, not a semantic guarantee \u2014 React may discard a cached value and recalculate in some circumstances (for example, to free memory for offscreen components), so code should never depend on <code>useMemo<\/code> for correctness the way you&#8217;d depend on a ref or actual state. Every demo above that relies on stability for correctness (like #2, #5, and #6) is really relying on the hook&#8217;s typical behavior, and would still need to be correct even if a recompute happened more often than expected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What does React.memo&#8217;s &#8220;shallow comparison&#8221; actually mean?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>React.memo<\/code> compares each prop individually to its previous value using <code>Object.is<\/code> \u2014 the same check used for dependency arrays. Primitives (numbers, strings, booleans) compare by value, so <code>5 === 5<\/code> is true regardless of how the 5 was produced. Objects, arrays, and functions compare by reference, so two objects with identical contents are never equal unless they&#8217;re the literal same object in memory \u2014 which is exactly the gap demo #2 and demo #6 both exploit and then close.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is useMemo the same thing as React Compiler&#8217;s automatic memoization?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Conceptually related, mechanically different. React Compiler (stable since its 1.0 release in late 2025, and opt-in per project) analyzes your component code at build time and inserts memoization automatically, aiming to make manual <code>useMemo<\/code>\/<code>useCallback<\/code> calls unnecessary in the components it covers. Every demo in this post uses the hooks manually and explicitly, which is still the only option outside a React Compiler-enabled build, and understanding what the hooks actually do is exactly what makes it possible to tell whether an automated tool got it right.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does the demo cap the broken effect at 40 runs instead of letting it run free?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Demo #5&#8217;s broken side is a genuine infinite loop \u2014 without a cap, it would re-render as fast as the browser can manage, which would pin a tab at high CPU and make the rest of the page unresponsive. The cap exists purely so you can Fork the demo, intentionally remove the fix, and see the failure mode safely; the underlying bug is exactly as unbounded in real code as it looks here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The fastest way to internalize the difference between these two hooks is to Fork &amp; Edit demo #7 \u2014 the side-by-side one \u2014 and add a third column that wraps the calculation in neither hook at all. Watch its compute counter behave identically to the <code>useCallback<\/code> column, not the <code>useMemo<\/code> one, and the distinction stops being something you have to remember. Once you&#8217;ve got a version you like, save it to <a href=\"https:\/\/webdevpuneet.com\/ui-snippets\/mycode\/\">My Code<\/a> and it&#8217;s yours to keep, tweak further, or drop into a real project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Recommended next step:<\/strong> open the <a href=\"https:\/\/webdevpuneet.com\/react-playground\/\">React Playground<\/a> \u2014 webdevpuneet&#8217;s full in-browser React sandbox \u2014 and rebuild demo #6&#8217;s parent-to-child memoization chain from scratch with your own data. Typing it out yourself, not just reading it, is what makes the pattern stick. For more tutorials like this one, browse the <a href=\"https:\/\/webdevpuneet.com\/blog\/category\/react\/\">React<\/a> category on the blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most explanations of useMemo and useCallback stop at &#8220;one caches a value, the other caches a function,&#8221; which is true and also not nearly enough to use them correctly. The interesting part is why that distinction matters \u2014 how React actually compares dependencies, what &#8220;referential equality&#8221; means for an object literal versus a number, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":179,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","advanced_seo_description":"useMemo vs useCallback explained with 8 live re-render demos: React.memo, stale dependencies, premature memoization, effect loops and why memo isn't a cache.","jetpack_seo_html_title":"useMemo vs useCallback: What They Actually Do (8 Demos)","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"React developers: Do you really understand `useMemo` vs `useCallback`?\n\nBoth are used for performance optimization, but they solve different problems. And using them incorrectly can make your code more complicated without improving performance.\n\nIn this tutorial, explore **8 interactive React demos** that show what actually happens during re-renders:\n\n* `useMemo` vs `useCallback` \u2014 caching values vs function references\n* How inline functions can break `React.memo`\n* Why missing dependencies cause stale values\n* When memoizing a cheap calculation is pointless\n* How `useCallback` can prevent unnecessary effect re-runs\n* Why an unstable object can break downstream memoization\n* Why `useMemo` remembers the last result, not every previous result\n* How to identify unnecessary recalculations and re-renders\n\nEvery example includes live demos so you can interact with the code, trigger re-renders, and watch the counters change in real time.\n\nStop guessing when to use React hooks. Understand what they actually do.\n\nRead the full tutorial:\n\n#ReactJS #JavaScript #WebDevelopment #FrontendDevelopment #ReactHooks\n","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","et-has-post-format-content","et_post_format-et-post-format-standard"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/phrh5V-2S","jetpack_featured_media_url":"https:\/\/i0.wp.com\/webdevpuneet.com\/blog\/wp-content\/uploads\/2026\/10\/usememo-vs-usecallback-featured-1200x630-compressed.webp?fit=1200%2C630&ssl=1","_links":{"self":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":1,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions\/180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/media\/179"}],"wp:attachment":[{"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdevpuneet.com\/blog\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}