<?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 Om Dwivedi on Medium]]></title>
        <description><![CDATA[Stories by Om Dwivedi on Medium]]></description>
        <link>https://medium.com/@omkkdwivedi?source=rss-ffaf8675b621------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qXgevYdP09bVYtD1</url>
            <title>Stories by Om Dwivedi on Medium</title>
            <link>https://medium.com/@omkkdwivedi?source=rss-ffaf8675b621------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:29:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@omkkdwivedi/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 Is a Lie — And You’re Probably Using It Wrong]]></title>
            <link>https://medium.com/@omkkdwivedi/react-memo-is-a-lie-and-youre-probably-using-it-wrong-32fad13cc19d?source=rss-ffaf8675b621------2</link>
            <guid isPermaLink="false">https://medium.com/p/32fad13cc19d</guid>
            <category><![CDATA[react-memo]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Om Dwivedi]]></dc:creator>
            <pubDate>Mon, 20 Apr 2026 17:14:04 GMT</pubDate>
            <atom:updated>2026-04-20T17:14:04.846Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f9XYZaFrf3eoXEfiYC9DAw.png" /></figure><p><em>You wrapped your component in React.memo. You patted yourself on the back. It’s still re-rendering on every single update. Here’s why.</em></p><p>There’s a certain confidence that comes with writing React.memo(MyComponent). It feels like optimization. It looks like you know what you&#39;re doing. Senior devs nod approvingly during code review.</p><p>And then you open React DevTools Profiler and watch your “memoized” component re-render 47 times in three seconds.</p><p>This isn’t a rare edge case. It happens constantly, in production codebases, written by experienced developers. The problem isn’t React.memo itself — it’s a fundamental misunderstanding of what it actually checks.</p><h3>What React.memo Actually Does</h3><p>React.memo is a Higher Order Component that prevents re-renders by comparing the <strong>previous props</strong> with the <strong>new props</strong>. If they’re the same, it skips the render.</p><p>The keyword is <em>same</em>. And in JavaScript, “same” is more complicated than it sounds.</p><p>React.memo uses <strong>shallow equality</strong> by default. For primitive values — strings, numbers, booleans — this works exactly how you’d expect. &quot;hello&quot; === &quot;hello&quot; is true. 42 === 42 is true.</p><p>But for objects and functions, shallow equality checks the <strong>reference</strong>, not the value. Two objects that look identical are not equal unless they point to the same place in memory.</p><pre>{ name: &quot;Om&quot; } === { name: &quot;Om&quot; }  // false</pre><p>That’s it. That’s the whole trap.</p><h3>The Bug You Don’t Know You Have</h3><p>Here’s the most common version of this mistake:</p><pre>function ParentComponent() {<br>  const [count, setCount] = useState(0);<br><br>  const config = { theme: &quot;dark&quot;, language: &quot;en&quot; };<br><br>  return (<br>    &lt;&gt;<br>      &lt;button onClick={() =&gt; setCount(c =&gt; c + 1)}&gt;Increment&lt;/button&gt;<br>      &lt;MemoizedChild config={config} /&gt;<br>    &lt;/&gt;<br>  );<br>}<br><br>const MemoizedChild = React.memo(function Child({ config }) {<br>  console.log(&quot;Child rendered&quot;);<br>  return &lt;div&gt;{config.theme}&lt;/div&gt;;<br>});</pre><p>Every time you click the button, ParentComponent re-renders. A new config object is created from scratch — same values, brand new reference. React.memo compares the old reference with the new one, sees they&#39;re different, and re-renders MemoizedChild.</p><p>Your memoization is doing absolutely nothing.</p><h3>Functions Are Even Sneakier</h3><p>If objects are the obvious case, functions are the silent killer. And they show up everywhere.</p><pre>function Parent() {<br>  const [count, setCount] = useState(0);<br><br>  const handleClick = () =&gt; {<br>    console.log(&quot;clicked&quot;);<br>  };<br><br>  return (<br>    &lt;&gt;<br>      &lt;button onClick={() =&gt; setCount(c =&gt; c + 1)}&gt;Increment&lt;/button&gt;<br>      &lt;MemoizedChild onClick={handleClick} /&gt;<br>    &lt;/&gt;<br>  );<br>}</pre><p>Every render creates a brand new handleClick function. Brand new reference. React.memo fails. Again.</p><p>This is why useCallback exists. But even useCallback is misunderstood.</p><h3>The Fix — And the Next Trap</h3><p>The correct approach:</p><pre>function Parent() {<br>  const [count, setCount] = useState(0);<br><br>  const config = useMemo(() =&gt; ({ theme: &quot;dark&quot;, language: &quot;en&quot; }), []);<br><br>  const handleClick = useCallback(() =&gt; {<br>    console.log(&quot;clicked&quot;);<br>  }, []);<br><br>  return (<br>    &lt;&gt;<br>      &lt;button onClick={() =&gt; setCount(c =&gt; c + 1)}&gt;Increment&lt;/button&gt;<br>      &lt;MemoizedChild config={config} onClick={handleClick} /&gt;<br>    &lt;/&gt;<br>  );<br>}</pre><p>Now config and handleClick have stable references across renders. React.memo&#39;s shallow comparison passes. The child doesn&#39;t re-render.</p><p>But here’s the next trap: <strong>the dependency array</strong>.</p><pre>const handleSubmit = useCallback(() =&gt; {<br>  submitData(userId, formData);<br>}, []); // ← empty deps</pre><p>This looks stable. It is stable. It is also <strong>stale</strong>. If userId or formData changes, handleSubmit will still close over the original values from the first render. You&#39;ve traded a re-render bug for a stale closure bug — which is harder to catch and more dangerous.</p><p>The correct version:</p><pre>const handleSubmit = useCallback(() =&gt; {<br>  submitData(userId, formData);<br>}, [userId, formData]);</pre><p>But now if formData is an object built inline, it changes reference on every render, your dependency array always updates, useCallback returns a new function every time, and you&#39;re back to square one.</p><h3>The Real Mental Model</h3><p>Here’s how to actually think about this:</p><p><strong>React.memo</strong> asks: “Did the props change?” <strong>JavaScript</strong> answers: “Did the references change?” <strong>Your job</strong> is to make sure references only change when values actually change.</p><p>That’s the whole game. Everything else follows from it.</p><p>For primitive props, you don’t need to do anything. For object and function props, you need to stabilize their references using useMemo and useCallback — with correct, complete dependency arrays.</p><h3>When React.memo Is Actually Worth It</h3><p>Not every component needs memoization. In fact, adding it carelessly makes your code harder to read and your bundle slightly heavier, for zero benefit.</p><p>React.memo makes sense when:</p><p><strong>The component is expensive to render.</strong> Complex calculations, large lists, heavy DOM trees. If the render is cheap, the overhead of comparison might cost more than just re-rendering.</p><p><strong>The props are genuinely stable.</strong> If the parent re-renders often but the props rarely change, memoization saves real work. If the props change every time anyway, you gain nothing.</p><p><strong>You’ve confirmed the re-render is the actual bottleneck.</strong> Profile first. Guess second, never.</p><p>The worst use of React.memo is wrapping everything by default “just to be safe.” It’s not safer. It’s just more code to maintain with a false sense of optimization.</p><h3>The Tool You Should Actually Use First</h3><p>Before reaching for React.memo, useMemo, or useCallback, open <strong>React DevTools Profiler</strong>.</p><p>Record a session. Look at which components re-render and why. DevTools will show you exactly what prop changed and trigger the re-render. This tells you what actually needs fixing — and what doesn’t.</p><p>Premature memoization is its own performance problem. You create stable references with useCallback, pass them into useMemo, wrap the result in React.memo, and six months later nobody can figure out why the state isn&#39;t updating because somewhere in that chain, a dependency array is lying.</p><h3>The Summary No One Puts in the Docs</h3><p>React.memo uses shallow equality. Shallow equality checks references. References for objects and functions change on every render unless you stabilize them. useMemo stabilizes objects. useCallback stabilizes functions. Both require accurate dependency arrays. Inaccurate dependency arrays cause stale closures. Stale closures are worse than re-renders.</p><p>Profile before you optimize. The bug is usually not where you think it is.</p><p><em>If this saved you an afternoon of debugging, share it. If you’ve been burned by a different React optimization trap, drop it in the comments — there are several more where this came from.</em></p><p><strong>Tags:</strong> React, JavaScript, Web Development, Frontend, Performance</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32fad13cc19d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your app is lying to your users. And that’s a good thing.]]></title>
            <link>https://medium.com/@omkkdwivedi/your-app-is-lying-to-your-users-and-thats-a-good-thing-53f17dc953da?source=rss-ffaf8675b621------2</link>
            <guid isPermaLink="false">https://medium.com/p/53f17dc953da</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[optimistic-ui]]></category>
            <dc:creator><![CDATA[Om Dwivedi]]></dc:creator>
            <pubDate>Thu, 02 Apr 2026 17:55:58 GMT</pubDate>
            <atom:updated>2026-04-02T17:55:58.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YdENH9ikTcHNLgxgBDEfhA.png" /><figcaption>Optimistic ui: generated by nano banana</figcaption></figure><blockquote>The psychology and engineering behind Optimistic UI, the pattern that makes every great mobile app feel faster than it actually is.</blockquote><p>You tap the heart on an Instagram post. It turns red. Instantly. No spinner. No waiting. It just works</p><p>But the actual story is: Instagram has no idea yet whether you actually liked it or not. The API call hasn’t returned. The server might fail. Your connection might drop. And yet the heart is still red.</p><p>Instagram lied to you. And it felt great.</p><p>This is <strong>Optimistic UI, a deceptively simple pattern that separates apps that feel native and fast from apps that feel sluggish and web like. And once you see it, you can’t unsee it.</strong></p><h4>The two worlds: normal vs optimistic</h4><p>Most developers learn the “honest” approach first, update the UI only after the server confirms. It’s safe, it’s correct, and it makes user wait. The optimistic approach flips this entirely.</p><pre>+--------------------------------+-----------------------------------------+<br>|          NORMAL FLOW           |             OPTIMISTIC FLOW             |<br>+--------------------------------+-----------------------------------------+<br>| 1) User taps the like button   | 1) User taps the like button            |<br>| 2) App sends API request       | 2) UI updates instantly                 |<br>| 3) Spinner shows while waiting | 3) API requests fires in the background |<br>| 4) Server responds (200ms-2s)  | 4a) Server confirms - nothing to do     |<br>| 5) UI finally updates          | 4b) Server fails - rollback UI silently |<br>+--------------------------------+-----------------------------------------+</pre><p>The key insight is that step 4b — the failure path — is rare. Most API calls succeed. so why should 100% of your users wait for 100% of interaction just because 1% might fail?</p><blockquote>“Optimistic UI is a bet. You’re wagering that the server will agree with you and you are almost always right.”</blockquote><h4>You already use apps that do this</h4><p>Once you know the pattern, you start spotting it everywhere. These aren’t edge cases they’re the most-used apps in the world.</p><pre><br>+---------------------+----------------------+-----------------------+---------------------+<br>|      Instagram      |       Whatsapp       |         Gmail         |       Spotify       |<br>+---------------------+----------------------+-----------------------+---------------------+<br>| Likes, saves,       | Message appears with | Archive, delete, and  | Save to library     |<br>| and follows         | a single tick before | and label show        | responds before     |<br>| update before       | even leaving your    | instantly with an     | the server confirms |<br>| the server responds | device               | undo option           | the change          |<br>+---------------------+----------------------+-----------------------+---------------------+<br><br></pre><p>Notice what they all share: the actions are low stakes, high frequency, and reversible. That’s not a coincidence it’s exactly where this pattern shines.</p><h4>When to use it and when to absolutely not</h4><p>Use it when:</p><ul><li>The action is reversible</li><li>Failure rate is very low</li><li>Speed matters to UX</li><li>The action is simple (like, save, follow)</li><li>Users repeat the action often</li></ul><p>Avoid it when:</p><ul><li>The action has financial consequences</li><li>Failure has legal implications</li><li>Order matters (e.g. message sequencing)</li><li>The action is Irreversible</li><li>You are writing to multiple services</li></ul><p>You would never apply this to a payment confirmation, a contract signing, or deleting a user account. The potential cost of a failed rollback is too high. But for a social feed, a to-do list, or a settings toggle? It’s almost always the right call.</p><h3>The part everyone skips: the rollback</h3><p>Most articles explain the “happy path” i.e tap, update, done. But the rollback is where the real engineering lives. What actually happens when the server says no?</p><pre>+------------+---------------------------------+<br>| t = 0ms    | User taps like                  |<br>| t = 10ms   | API request fires in background |<br>| t = 320ms  | Server returns 500 error        |<br>| t = 321ms  | State rolls back: likes=false   |<br>+------------+---------------------------------+</pre><p>The rollback itself is straightforward you just revert to the previous state. What’s hard is the <em>UX</em> of the rollback. If the heart snaps back suddenly and silently, users think the app is buggy. The craft is in making the failure feel graceful.</p><p>The golden rule of rollbacks</p><blockquote>Never silently revert without telling the user. A brief toast message “Couldn’t save. Tap to retry.” turns a confusing moment into a recoverable one. The user stays in control.</blockquote><h3>The edge cases that will bite you</h3><p>Once you get past the basic pattern, three scenarios will cause you real pain if you haven’t thought them through.</p><p>Rapid successive taps -</p><p>User likes, unlikes, likes again in under a second. Which state is “correct”? You need to debounce or track a pending request queue, otherwise your UI and server fall out of sync.</p><p>Going offline mid-action -</p><p>The optimistic update fires but the API call never even gets sent. You need to detect offline state and either queue the request or revert immediately silently retrying when connectivity returns.</p><p>Cascading state -</p><p>One action affects multiple UI elements liking a post also changes a counter, a user’s profile, and a feed sort order. If the rollback only reverts one piece of state, your UI becomes inconsistent.</p><h3>It’s not about speed. It’s about trust.</h3><p>Here’s what’s easy to miss: Optimistic UI isn’t fundamentally a performance technique. Your API calls take exactly the same time. Nothing got faster.</p><p>What changed is the user’s <em>perception</em> of the app. The app responded to them. It trusted that their action was right. That feeling of an app that listens instantly is what creates the sensation of quality that users describe when they say an app “feels native.”</p><blockquote>“The app didn’t get faster. The user’s anxiety disappeared. That’s not the same thing and it matters more.”</blockquote><p>This is a lesson that crosses far beyond mobile development. The best interfaces aren’t the fastest ones. They’re the ones that make users feel heard. Optimistic UI is the most direct way to build that feeling into your React Native app.</p><h3>Where this fits in your React Native journey</h3><p>This pattern sits squarely at the intersection of state management and networking two of the most important modules in any serious React Native course. It requires you to think about <em>local state</em> (what the user sees right now), <em>server state</em> (the source of truth), and the tension between them. Mastering this gap is what separates junior mobile developers from engineers who build apps people love to use.</p><p>The next time you use an app and something feels instantly responsive — no spinner, no delay stop for a second. Someone made a deliberate choice to trust you. That’s the pattern. Now you know its name.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53f17dc953da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>