<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mayank Tandon on Medium]]></title>
        <description><![CDATA[Stories by Mayank Tandon on Medium]]></description>
        <link>https://medium.com/@tandonmayank2021?source=rss-6bb028a39748------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Mayank Tandon on Medium</title>
            <link>https://medium.com/@tandonmayank2021?source=rss-6bb028a39748------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:26:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tandonmayank2021/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React.memo vs useMemo: Why Mutating State Causes Stale UI in React (and How useImmer Helps)]]></title>
            <link>https://medium.com/@tandonmayank2021/react-memo-vs-usememo-why-mutating-state-causes-stale-ui-in-react-and-how-useimmer-helps-34a9445ae384?source=rss-6bb028a39748------2</link>
            <guid isPermaLink="false">https://medium.com/p/34a9445ae384</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Mayank Tandon]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 09:22:14 GMT</pubDate>
            <atom:updated>2026-03-01T09:22:14.427Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E6CCkDBr7sDJCMCJU6MmKQ.png" /></figure><p>A deep dive into React.memo, useMemo, and the state mutation bugs that cause stale UI.</p><h3>Introduction</h3><p>Most React performance optimizations look simple: add React.memo, use useMemo, and reduce unnecessary re-renders.</p><p>But when React state is mutated instead of replaced, those optimizations can silently break — leaving your UI stale even though the data changed.</p><p>The reason lies in how React relies on shallow comparison and reference equality to decide when components should re-render.</p><p>This article breaks down why React.memo and useMemo solve different problems, how state mutation interferes with React’s rendering model, and how to fix stale UI bugs without sacrificing performance.</p><h3>Optimizing Computation vs Optimizing Rendering</h3><p>A small React optimization can quietly introduce stale UI.<br>It started with something very common:</p><pre>const rows = useMemo(() =&gt; users.map(transformUser), [users])</pre><p>The idea was simple — avoid recomputing derived data.</p><p>But this only avoided <strong>recomputation</strong>, not <strong>re-rendering</strong>.<br>The real issue turned out to be <strong>render propagation through a heavy component</strong>.</p><p>In many real-world dashboards and data-heavy interfaces, unnecessary re-renders of heavy components (tables, charts, grids) can accumulate and impact <strong>render performance</strong>, increase <strong>CPU work during reconciliation</strong>, and delay important user-facing metrics like <strong>Largest Contentful Paint (LCP)</strong>.</p><h3>Moving the Optimization Boundary</h3><p>So the optimization boundary moved from the value to the component:</p><pre>const Table = React.memo(TableImpl)</pre><p>Now React can skip rendering the table when users hasn’t changed.</p><p>This can significantly reduce unnecessary reconciliation work for large component trees and prevent heavy child components from re-rendering when their inputs haven’t actually changed.</p><p>But after replacing useMemo with React.memo, the UI showed <strong>stale data in a few cases</strong>.</p><h3>Where the Problem Appeared</h3><p>After debugging, the issue turned out to be how state was being updated:</p><pre>const [users, setUsers] = useState([])<br><br>function updateUser(id, updatedData) {<br>  const user = users.find(u =&gt; u.id === id)<br>  Object.assign(user, updatedData)   // mutation<br>  setUsers(users)                    // same reference<br>}</pre><p>From React’s perspective:</p><pre>prevUsers === nextUsers</pre><p>So React.memo skipped the render — even though the object inside the array had changed.</p><p>This creates a subtle class of bugs where the <strong>state has technically changed</strong>, but React’s shallow comparison cannot detect it. As a result, the UI may display <strong>stale data</strong>, while performance optimizations behave unpredictably.</p><h3>The Immediate Fix</h3><p>The immediate fix was to <strong>ensure a new reference</strong>:</p><pre>const updatedUsers = users.map(u =&gt;<br>  u.id === id ? { ...u, ...updatedData } : u<br>)<br>setUsers(updatedUsers)</pre><p>This avoids reference mismanagement and ensures React can correctly detect changes and trigger re-renders when necessary.</p><p>However, over time these updates can expand into <strong>nested spreads, duplicated variables, and increasingly verbose state logic</strong>, especially as state updates become more complex.</p><pre>setUsers(prev =&gt;<br>  prev.map(u =&gt;<br>    u.id === id ? { ...u, ...updatedData } : u<br>  )<br>)</pre><p>Beyond verbosity, this pattern can also increase the cognitive overhead of maintaining state logic, making updates harder to reason about in larger applications.</p><h3>A Cleaner Approach: Immer</h3><p>One pattern that keeps both <strong>reference correctness and state updates clean</strong> is switching the state layer to useImmer:</p><pre>const [users, updateUsers] = useImmer([])<br><br>function updateUser(id, updatedData) {<br>  updateUsers(draft =&gt; {<br>    const user = draft.find(u =&gt; u.id === id)<br>    Object.assign(user, updatedData)<br>  })<br>}</pre><p>Immer lets you write mutation-style updates while still producing a <strong>new immutable state tree</strong>, ensuring references change when data changes.</p><p>This preserves the guarantees required by React’s shallow comparison model, allowing optimizations like React.memo to behave reliably while keeping state update logic readable.</p><h3>The Takeaway</h3><p>Optimizations like React.memo work best when the <strong>state layer guarantees predictable reference behavior</strong>.</p><p>Otherwise performance optimizations quickly turn into <strong>complex state management just to keep references stable</strong>, and can even introduce subtle UI bugs that are difficult to debug.</p><p>In the next article, I’ll break down how useImmer maintains reference guarantees under the hood — and whether it’s actually worth using in production React apps.</p><p>If this kind of deep dive into rendering behavior and performance tradeoffs is useful, consider following for more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34a9445ae384" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>