Work with our skilled React developers to accelerate your project and boost its performance.
Hire Reactjs DevelopersSVG icons are popular in web development because they are lightweight, scalable, and easy to style. In React, there are several simple ways to display .svg files:
Import the SVG file directly and use it like a normal component:
import { ReactComponent as MyIcon } from './icons/my-icon.svg'; const MyComponent = () => <MyIcon width={24} height={24} />;
Note: Supported in Create React App (CRA) and Vite (with vite-plugin-svgr).
Treat the SVG like a regular image:
const MyComponent = () => ();
-> Very simple for static icons
-> Harder to change colors dynamically
Paste the SVG markup into your component:
const MyIcon = () => ( );
Load icons based on props:
const MyComponent = ({ iconName }: { iconName: string }) => { const Icon = require(`./icons/${iconName}.svg`).default; return; };
Or with Vite:
const icons = import.meta.glob('./icons/*.svg', { eager: true, import: 'default' }); const MyComponent = ({ iconName }: { iconName: string }) => { const Icon = icons[`./icons/${iconName}.svg`]; return; };
Choose the method that fits your needs and keep your UI lightweight and flexible!