How to convert SVG to React components
A comprehensive guide to converting SVG assets into flexible and maintainable React components, enhancing UI scalability and developer experience.
Introduction
SVGs (Scalable Vector Graphics) are an essential part of modern UI design. They offer resolution-independent, scalable, and lightweight graphics, making them ideal for icons and illustrations. However, directly embedding SVG code in HTML or JSX can quickly get messy and hard to manage. A more robust and scalable solution is converting SVGs into React components.
This case study walks through how I transformed raw SVG files into reusable and flexible React components, complete with good coding standards and practices. This not only improved maintainability but also enhanced developer experience when building interfaces.
Why Convert SVGs to React Components?
- Reusability: Wrap SVGs as components that can be reused across your app.
- Customizability: Easily apply props such as
className
,style
, or override attributes likestroke
,fill
, and even use text color utilities (liketext-red-500
) to style SVGs, keeping them consistent with your design system. - Type Safety: Leverage TypeScript to ensure proper typing using
React.SVGProps
. - Cleaner Codebase: Keep JSX clean and avoid cluttering files with raw SVG markup.
My Approach
1. Choosing the Source
I started with a set of SVG icons exported from Figma. These icons had inline styles, hardcoded dimensions, and were not optimized for React.
2. Cleaning Up SVG Code
Using tools like SVGO and SVGOMG, I optimized the SVG markup. This removed unnecessary metadata, reduced file size, and standardized formatting.
3. Creating a React Component Wrapper
I chose to use TypeScript for type safety and flexibility. Here's the standard structure I follow:
4. Standards I Followed
React.SVGProps<SVGSVGElement>
: This makes the component flexible by inheriting all standard SVG props.- Props Spread (
{...props}
): Allows consumers to override default props likewidth
,height
,stroke
, or attach event handlers. - Default Dimensions as
100%
: So the SVG can inherit size from parent containers. viewBox
: Always include this to ensure proper scaling.- No hardcoded styles: Styles can now be passed via
className
or inline styles. - Naming Convention: Icons are named as
PascalCase
withIcon
suffix (AppleIcon
,SearchIcon
, etc.).
Bonus: Auto-generating Icons
If you have a large set of SVGs, consider automating the conversion process with tools like:
svgr
– Converts SVGs into React components with options for TypeScript, icon mode, and more.
Conclusion
Converting SVGs into React components improved our codebase by making icons more maintainable, customizable, and consistent. With the right standards, naming conventions, and tooling, this becomes a scalable pattern that enhances both performance and developer experience.
If you're working on a design system or a component library, this approach will pay dividends in the long run. Start small, stay consistent, and consider automating the process for even better efficiency.