Misunderstanding how JavaScript handles functions can hurt your UI performance. In JavaScript, functions are compared by reference. And in React, every render creates new function instances. So even if a function looks exactly the same, it’s actually a different reference in memory. That means when you pass a function as a prop, React sees it as a new value on every render. And if that component is wrapped with React.memo, it will still re-render. Not because React.memo doesn’t work, but because the prop actually changed. This can be solved by keeping the same function reference between renders, which is exactly what useCallback helps you do. This doesn’t mean you should use useCallback everywhere. It only matters when you need a stable function reference, like when passing props to memoized components. I’ve been using these techniques for a while, but it took me some time to really understand why they are often used together. This is a good example of how understanding the fundamentals of JavaScript can directly impact performance and behavior in React applications. (Simplified example in the image) #React #JavaScript #Frontend #WebDevelopment #SoftwareEngineering
The post explains the underlying javascript behavior really well, functions are still compared by reference and recreated on each render… the react compiler just abstracts a lot of that away by handling memoization automatically. So the concept still matters, even if we don’t always have to optimize it manually anymore.
Don’t need to even do this with the new react compiler in react 19
I mean you can solve this by not using React…
Upgrade to 19. UseMemo and useCallBack are actually not free. Overused still hurts performance
Great insights! Thanks for sharing. 👍
Yes , exactly
Good post but only applies if new React compiler is not being used. React compiler automatically does these optimizations at build time.