Menu

Turning a folder of SVG icons into React components (without a build plugin)
πŸ“°

Turning a folder of SVG icons into React components (without a build plugin)

DEV Community: reactΒ·SVG to ReactΒ·about 1 month ago
#4eaVg6X2
#devtoreact#devto#dev#name#icons
Long article (1560 words) β€” no AI summary yet.
Reading 0:00
15s threshold

You have a folder of SVG icons from a designer, an icon set, or an old project. You want them as React components: <SearchIcon /> , <CloseIcon /> , each taking a className , a size , maybe a title . The usual answer is "add SVGR to your bundler." That works, but sometimes you can't or don't want to touch the build. Maybe it's a locked-down framework config, a design-system package that ships plain .js , or you just want readable component files in the repo that anyone can open and edit.

This post walks through a plain Node script that converts a directory of .svg files into .jsx components. There's no webpack loader or Vite plugin, and the only runtime dependency is React.

What actually changes between SVG and JSX

An SVG file is XML, and JSX is close to it, but not the same. A converter has to handle these differences:

Hyphenated attributes become camelCase.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.