PrettifyPrettify

SVG to React Component Converter

Paste an icon from a design tool and get a React component you can drop into a TypeScript project. SVG Tools in Prettify optimizes the markup with svgo, adds a prefix to every id so two icons cannot clash, and writes a typed component that accepts every SVG prop. It all runs in your browser.

Result
The source, viewBox, size, and ids show here as you paste. Open the Image view to see it drawn.
Runs in your browser. Nothing is uploaded.

What SVG to React does

An SVG from a design tool is rarely ready for React. It has an XML declaration, editor comments, attribute names such as stroke-width that JSX spells strokeWidth, and ids such as fade that clash when two icons share a page. SVG Tools fixes all of that in three steps and shows the result at once.

The box on this page checks your SVG and shows its viewBox, size, and ids. The conversion itself runs in SVG Tools in the editor. Select "Open in Image view", then select Tools in the toolbar.

The three steps

The SVG Tools panel runs the steps in order. It never changes your document until you select Apply to document.

  1. Optimize: svgo makes the SVG smaller, and the panel shows the bytes saved. Optimize also removes script elements, on* event attributes, and javascript: links, and tells you when it did.
  2. Unique IDs: Prettify adds a prefix to every id, every url(#...), every href="#...", and every class name. The default prefix comes from the document name, and you can edit it.
  3. React component: Prettify writes the result as a component. You can edit the component name.

The component you get

The component is an arrow function typed React.FC<React.SVGProps<SVGSVGElement>>, with export default. It spreads its props onto the svg element, so it accepts width, className, aria-label, and every other SVG prop.

  • The name comes from the file name: cloud-rain.svg gives CloudRain. For a pasted SVG, the name comes from its title element, or it is Icon when there is none.
  • The component keeps only known SVG elements and attributes. It drops script and unknown elements, event attributes such as onclick, and javascript: links.
  • Links and image URLs keep only #fragment references, relative URLs, and http and https URLs.
  • SMIL timing references, aria-labelledby, and aria-describedby stay valid after Unique IDs, and Optimize keeps role="img".
  • An Original and Result switch draws both versions, and a status says "Same drawing" or "Changed" after a pixel comparison.

How to convert an SVG to React

Start in the box on this page, which reads SVG up to 512 KB. For an SVG over 512 KB, the panel runs the steps only when you select Run.

  1. Paste your SVG into the box labeled "Paste your SVG", or select "Try an example".
  2. Check the line above the result. If the SVG cannot be drawn, fix the line it names first.
  3. Select "Open in Image view", then select Tools in the toolbar.
  4. Leave Optimize and Unique IDs on, and check the prefix and the component name.
  5. Select the React tab to read the component, then select Copy React.
  6. To keep the optimized SVG in the document too, select Apply to document.

Worked example: a design tool icon

The example in the box is the icon below. Its title is Cloud rain, so the panel names the component CloudRain and uses the prefix cloud_rain_. The panel warns "This SVG has scripts. Optimize removes them." The Optimize step shows 588 B to 452 B, 23% smaller: it removes the XML declaration, the comment, and the onload attribute. The longer ids from Unique IDs make the final SVG 474 B.

In the component, the gradient id fade becomes cloud_rain_fade, and the fill that points to it changes to match. JSX names replace the attribute names: stop-color becomes stopColor, and stroke-width becomes strokeWidth. The svg element ends with {...props}, so <CloudRain className="icon" /> works.

const CloudRain: React.FC<React.SVGProps<SVGSVGElement>> = (
  props: React.SVGProps<SVGSVGElement>
): React.ReactElement => (
  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" {...props}>
    <title>{'Cloud rain'}</title>
    <defs>
      <linearGradient id="cloud_rain_fade" x1="0" x2="0" y1="0" y2="1">
        <stop offset="0" stopColor="#60a5fa" />
        <stop offset="1" stopColor="#2563eb" />
      </linearGradient>
    </defs>
    <path fill="url(#cloud_rain_fade)" d="M7 15h10a4 4 0 0 0 0-8A6 6 0 0 0 5.7 9 3 3 0 0 0 7 15" />
    <path stroke="currentColor" strokeLinecap="round" strokeWidth="2" d="M8 18v2m4-2v3m4-3v2" />
  </svg>
);

export default CloudRain;

Common uses

Teams that keep icons as components want each one in the same shape, with no ids that clash and nothing unsafe inside.

  • Add an icon from a design tool to a React and TypeScript project.
  • Put several icons on one page without gradient or mask ids that clash.
  • Remove scripts and event attributes from an SVG you did not make.
  • Make an icon smaller before you commit it, and check that it still draws the same.

Free and Pro

SVG Tools is part of Pro, a one-time purchase of US$19.99 with lifetime access and no subscription. Without Pro, the panel still shows how much smaller Optimize can make the SVG, for example "This SVG can be 48% smaller with SVG Tools." The SVG viewer, with zoom, backgrounds, and the size strip, is free.

Your SVG stays on your device. Optimize, Unique IDs, and the component all run in your browser.

Frequently asked questions

Is SVG to React free?

SVG Tools, which writes the component, is a Pro feature. Pro costs US$19.99 once. Viewing the SVG, checking it in the box on this page, and Download are free.

Does the component work with TypeScript?

Yes. It is typed React.FC<React.SVGProps<SVGSVGElement>> and returns a React.ReactElement. It accepts every SVG prop, such as width, height, and className, because it spreads props onto the svg element.

Why are my ids changed?

An inline SVG shares the page with other SVGs, so two icons can use the same id. Unique IDs adds a prefix to each id and to every reference to it. You can edit the prefix or turn the step off.

What does the converter remove?

Script elements, unknown elements, event attributes such as onclick, and javascript: links. Links and image URLs keep only #fragment, relative, http, and https URLs. CSS nesting and media range syntax in a style element are dropped from the React output.

How do I check that the optimized icon looks the same?

Use the Original and Result switch in the toolbar to draw each version. The status next to it says "Same drawing" or "Changed", from a comparison of the pixels at the same size.

Related tools