React Compiler Misses Class Changes: A Hidden Dependency Issue

How React Compiler Misses Class Changes? . Recently I was cleaning up useMemo after turning on React Compiler in a project. The compiler’s supposed to handle most of that now. React memoization, either with compiler or manual, uses reference equality. If object reference changes, it assumes dependency changed. I had a component which was accepting class instance as props. The state logic was returning a new class instance each update with same internal string value. Only object instance was new. The mystery I learnt about the React compiler was that it can’t look into private field and say “ah, string’s stable.” So from its point of view, when class instance changed even with same inernal values, input changed. And technically it did when we created different object. I tried switching to plain data and a pure helper function. Memoization started working as expected. And I mean, it’s not that classes are bad. It’s that hidden dependencies don’t work smoothly with reference checks. I assumed compiler would be smarter. That’s on me. Now when I pass a class instance to a component as props, I kind of stop there and think again. Love more content of this type? Follow me here: https://lnkd.in/gyrM-Gpt #react #javascript #typescript #web

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories