About SVG to React Component
Paste an icon or illustration as SVG, or open an .svg file, and get a React function component you can drop into a project. Attributes are renamed the way JSX expects (class to className, stroke-width to strokeWidth, xlink:href to xlinkHref), inline styles become style objects, and text and style sheets are escaped so they compile.
Optionally the fixed width and height become a size prop, and every fill and stroke color becomes currentColor driven by a color prop, so one component serves every size and theme. Editor leftovers, comments, scripts and event handlers are left out.
How it works
- The SVG is read as XML in your browser. A file that is not well-formed is reported with the line and column of the problem.
- Each attribute is renamed to its React prop. Namespaced editor attributes (inkscape:, sodipodi: and the like), xmlns declarations, event handlers and javascript: links are dropped.
- With the size prop, the root’s width and height are replaced by size, and a viewBox is added from them if the SVG had none, so the drawing scales.
- With the color prop, fill and stroke colors become currentColor; none, url() references and gradient stop colors are kept as they are.
Questions
Should I optimize the SVG first?
It helps. Exports from drawing apps often carry hidden groups, long decimals and unused definitions. Run the file through the SVG optimizer on this site first, then convert the smaller result.
What happens to a multicolor icon?
Turn the color prop off and its colors are kept as written. With the color prop on, every fill and stroke color becomes the single color you pass in.
Will two copies of the component clash?
They can if the SVG uses ids, for gradients, masks or clip paths, because ids are shared across the page. Icons without defs are unaffected. For ones with defs, rename the ids per use.
How large an SVG can it convert?
Up to 2 MB of markup, which covers icons and most illustrations. Nothing is uploaded: the conversion runs in your browser.
Everything you enter here is processed in this browser tab. Nothing is uploaded, logged or shared. How privacy works here.