r/learnreactjs • u/its-procodester • Aug 15 '24
Question Wrap SVG in React.memo?
Is it beneficial to wrap an SVG component in React.memo to prevent unnecessary re-renders, considering they are pure components?
2
Upvotes
View all comments
Show parent comment
1
u/detached_obsession Aug 26 '24
Of course it matters if it's causing issues. If you use React.memo on an SVG component and that component takes in a prop that changes constantly, your memoization isn't doing anything to help. Finding root causes and addressing them is far more valuable than doing preemptive optimizations.
Now I'm not saying you should never memoize, I'm saying it should be used to address specific issues.
If that svg is truly the cause of your slow performance then by all means memoize it. But, if a simple icon is causing issues, why stop there? Why not use uncontrolled components with refs and event delegation to prevent re-renders of the input in general?
Yes, everyone should try to write performant code, but saying that you can achieve this by applying memoization without reason, in my opinion, is incorrect.
Don't get me wrong, you certainly make valid points. I only disagree on using memoization every time without a specific need.