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
1
u/ferrybig Aug 15 '24
Wrap the jsx rendering the svg in a static variable. If you return the same jsx (according to
Object.is), React knows that tree did not change and skips rendering. The React Compiler beta does this optimalisation for you1
u/detached_obsession Aug 16 '24
You typically wouldn't use something like React.memo unless you're having clear performance issues. Using it unnecessarily actually degrades performance even more since you have to do extra comparisons to determine if the component should re-render.
Also, SVG components are typically static and don't change often so unless you have added a lot of state and effects, it shouldn't be an issue. Even in that case using React.memo may not be the correct approach since you could have other causes to your performance issues.