Believe it or not, you don't need extra dependencies to add icons in your App as actual components. Be it Svelte, React, or SolidJS.
#This is the BEST way to add icons.
I'll share with you my best-practice for adding icons in your App. I currently use these three frameworks: React, SolidJS, and Svelte, so I'll show you how to add and manage your icons in each of those projects, without compromising on flexibility..
Go to Icones.js.org and search for the icon you want to add. Then click on the SVG button under "Snippets". For instance, I'll use this Marker Duotone icon. (The svg would be copied to your clipboard)
#Second, add it to your JS app.
Now that you've copied the SVG to your clipboard, you can add it to your app. I'll show you how to do it in React, SolidJS, and Svelte. I usually store my icons in this structure:
You really only need to add {...props} and add the SVG attributes to the props types.
#React
Create a new .tsx for your icon and extend the props with SVGProps<SVGSVGElement>:
Prefix the named export with Icon it's easy to find with intellisense:
#Svelte 5
Create a new .svelte for your icon and extend the props with SVGAttributes<SVGSVGElement>:
Prefix the named export with Icon it's easy to find with intellisense:
#SolidJS
Create a new .tsx for your icon and extend the props with JSX.SvgSVGAttributes<SVGSVGElement>:
Prefix the named export with Icon it's easy to find with intellisense:
#Using your Icons
🥳 Congrats, you've added your icons! It's as easy as doing this now:
#BONUS: Just save them as .svg files.
There's a much easier way by taking advantage of Vite Plugins. Unfortunately, this only works for React (vite-plugin-svgr) and SolidJS (vite-plugin-solid-svg). But this way, you can store your icons like so:
#Use .svg-only with SolidJS
Install vite-plugin-solid-svg.
Add it to your vite.config.ts file.
Usually, .svg imports are 'string' types, not JSX. To fix this, add this to tsconfig.json:
In your assets/index.ts file, you can import the .svg this way:
#Use .svg-only with React
Install vite-plugin-svgr.
Add it to your vite.config.ts file.
Usually, .svg imports are 'string' types, not JSX. To fix this, add this to tsconfig.json:
In your assets/index.ts file, you can import the .svg this way: