Convert SVG to JSX for React
SVG pasted straight into React gives warnings such as Invalid DOM property class and stroke-width. JSX wants class as className, hyphenated attributes in camelCase, and style as an object. This SVG to JSX converter makes those changes. The Sample:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Exported from a design tool -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9" fill="none" stroke="teal" stroke-width="2"/>
<path data-name="check" d="M8 12.5l2.5 2.5L16 9.5" fill="none" stroke="teal" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="opacity: 0.9"/>
</svg>becomes:
<svg
xmlns="http://www.w3.org/2000/svg"
className="icon"
width="24"
height="24"
viewBox="0 0 24 24"
>
<circle cx="12" cy="12" r="9" fill="none" stroke="teal" strokeWidth="2" />
<path
d="M8 12.5l2.5 2.5L16 9.5"
fill="none"
stroke="teal"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
style={{ opacity: "0.9" }}
/>
</svg>The XML declaration, the comment and the data-name attribute are removed, and 5 attributes are renamed.
What Gets Renamed
- class to className, for to htmlFor, tabindex to tabIndex.
- Hyphenated attributes to camelCase: stroke-width to strokeWidth, fill-rule to fillRule, clip-path to clipPath, stop-color to stopColor.
- xlink:href to xlinkHref, xml:space to xmlSpace, xmlns:xlink to xmlnsXlink.
- style="fill: red; stroke-width: 2" to style={{ fill: "red", strokeWidth: "2" }}.
- Text with { or } is escaped so JSX does not read it as code, and the contents of a <style> element become a template string.
data-* and aria-* attributes stay as they are, and attributes that are already camelCase in SVG, such as viewBox and gradientUnits, are not touched.
Make a React Component
Set Output to TypeScript component, type a name, and switch on Remove width and height and Use currentColor. The Sample becomes:
import type { SVGProps } from "react";
const CheckCircle = (props: SVGProps<SVGSVGElement>) => (
<svg xmlns="http://www.w3.org/2000/svg" className="icon" viewBox="0 0 24 24" {...props}>
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" strokeWidth="2" />
<path
d="M8 12.5l2.5 2.5L16 9.5"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
style={{ opacity: "0.9" }}
/>
</svg>
);
export default CheckCircle;Because of {...props} you can write <CheckCircle className="size-6 text-teal-600" aria-label="Done" />. With currentColor the icon takes the text color of its parent, and without width and height CSS controls its size.
Safe Output
<script> elements, event attributes such as onclick or onload, and links that start with javascript: are always removed from the output. Editor attributes such as inkscape:label and sodipodi:docname are removed because React cannot render namespaced attributes. The preview is drawn as an image, so scripts in the SVG never run.
Going the Other Way and Other SVG Tools
Turn JSX back into a plain file with JSX to SVG, or press the swap button. To get an image from the same code use SVG to PNG, and for Android use SVG to XML.
Privacy
The conversion runs in your browser. Your SVG is not uploaded. See the Privacy Policy.