React.memo Not a Performance Shield: The Ghost Render Trap

Why React.memo isn't a "Magic Shield" for Performance 🛡️💥 Have you ever wrapped a component in React.memo, only to see it re-rendering anyway in the Profiler? You aren't alone. This is the "Ghost Render" trap, and it’s one of the most common performance killers in large-scale React apps. The "Perception" Myth 🧠 We often think: "I'm passing style={{ color: 'red' }} (or a callback function). The value is still red, so the child shouldn't re-render." The "JS Engine" Reality ⚙️ JavaScript doesn't care that the values inside the object are the same. It cares about the Memory Address. → Every time the Parent renders, that inline object {} is created as a new reference. → In JS, {} === {} is FALSE. → React.memo sees this new reference and assumes the prop has changed. The "shield" is bypassed, and the child re-renders. The Pro Fix ✅ To keep the "shield" strong, you must maintain Referential Integrity. → Use useMemo for objects/arrays. → Use useCallback for functions. This ensures the memory address stays the same across renders. As shown in the diagram, stableStyle === stableStyle becomes TRUE, and React successfully skips the unnecessary re-render. Watch the 30sec video for visual explanation : https://lnkd.in/gs2iwP92 #ReactJS #JavaScript #WebPerformance #SoftwareEngineering #Frontend #CodingTips #CleanCode #Programming

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories