2+ of experience in software development with a focus on React, Redux, JavaScript, Cypress, Docker, Figma, Design System.
MSc student in Computer Engineering.
A small note for anyone who tries useState instead of useRef: React treats a function passed to a setter as an updater, so setSvgIcon(ReactComponent) will call the component instead of storing it. Wrap it in an arrow function to fix it:
setSvgIcon(()=>ReactComponent);
useRef skips this because assigning to ref.current is a plain assignment.
Also, if you’re on vite-plugin-svgr v4+, import with ?react and use the default export instead of ReactComponent.
I oversee all things software at recruitment scale-up Harver. Always looking for new colleagues who want to work in an environment that values great software and great engineers.
Did I say anywhere in my comment that it was my code? Also, I know it’s probably too hard for you, but you can press the ‘Reply’ button to directly reply. Have a nice day.
Vite (Rollup) sees import(../../assets/icons/${iconName}.svg) and splits every SVG in that folder into its own chunk. None of them land in the main bundle, so the initial load stays small.
An icon is only downloaded when it’s first rendered, then cached.
Cons:
Since the name is dynamic, Vite can’t tell which icons you use, so all icons in that folder get built as chunks, even unused ones. Build output grows, but users only download what they render.
Each icon is a separate request, so a page with many different icons means many small requests (and a brief shimmer on first load).
Each chunk is a React component (JS), so it’s slightly larger than the raw .svg.
Tip: for icons shown on every page (logo, nav), import them statically so they ship in the main bundle and don’t flash a loader. Keep the dynamic folder limited to icons that really are on-demand.
We're a place where coders share, stay up-to-date and grow their careers.
Amazing!
I really appreciate you :)
I have a question why this solution does not work when we use
useStateinstead ofuseRef?Sorry for late comment.
A small note for anyone who tries useState instead of useRef: React treats a function passed to a setter as an updater, so setSvgIcon(ReactComponent) will call the component instead of storing it. Wrap it in an arrow function to fix it:
useRef skips this because assigning to ref.current is a plain assignment.
Also, if you’re on vite-plugin-svgr v4+, import with ?react and use the default export instead of ReactComponent.
The author isn't going to answer questions because he doesn't know. He just stole the code from: StackOverflow without crediting the author. Shameful.
great article!
Thanks for sharing, great article.
Thank you @mondal10, so helpful, you really rescued me in despair.
Thanks @mondal10. Very helpful!
Lmao @jradams1997 pwned
Did I say anywhere in my comment that it was my code? Also, I know it’s probably too hard for you, but you can press the ‘Reply’ button to directly reply. Have a nice day.
Toxic mate
How it influence bundle size ?
It’s a mixed bag, but mostly good.
Pros:
Cons:
Tip: for icons shown on every page (logo, nav), import them statically so they ship in the main bundle and don’t flash a loader. Keep the dynamic folder limited to icons that really are on-demand.