What is an SVG to JSX converter?
You can paste SVG markup into a React file, but it will not compile as is. JSX wants className instead of class, strokeWidth instead of stroke-width, a style object instead of a style string, and braces and angle brackets in text must be escaped. Icons exported from Figma, Illustrator or Inkscape also carry XML prologs, comments and editor namespaces that you do not want in a component. This tool turns SVG code into a React component, in JavaScript or TypeScript, or into plain JSX markup.
What it does to the code: it renames attributes to camelCase (class to className, xlink:href to xlinkHref, xmlns:xlink to xmlnsXlink), keeps data- and aria- attributes as they are, turns numeric values such as width and strokeWidth into {24} and {2}, and converts style="..." into a style object. It drops comments, the XML prolog, <script> and <metadata> elements, event handlers such as onload, and editor namespaces like inkscape: and sodipodi:. CSS inside <style> goes into a template string. If the namespace is missing, it adds it so the file parses.
An "icon mode" sets the size to 1em and turns every color except none, inherit and url(...) into currentColor, so the icon follows the text color and size. It all runs in your browser, so your artwork stays private.
How to convert SVG to a React component
- Paste your SVG into "SVG code", or press "Open an .svg file" to load one. The preview above the result shows the drawing.
- In "Result", choose "Component" for a full function component or "JSX only" for just the markup.
- For a component, pick the "Language": JavaScript or TypeScript. TypeScript adds
import type { SVGProps } from 'react'and types the props. - Type the "Component name". It is turned into a valid PascalCase name, so
arrow-leftbecomesArrowLeft. - Tick or untick "Pass props to the svg (size, color, className)". When ticked,
{...props}goes on the rootsvg. - Tick "Icon mode (1em size and currentColor)" if the SVG is an icon that should adapt to the text.
- Copy the JSX from the output box. If the SVG is not valid XML, a message shows the line and column instead.
Examples
A component in JavaScript
Input:
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24" fill="none" stroke="#141413" stroke-width="2" stroke-linecap="round"><path d="M5 12h14" style="stroke-dasharray: 4 2"/></svg>Output with the defaults:
const Icon = (props) => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="icon"
viewBox="0 0 24 24"
fill="none"
stroke="#141413"
strokeWidth={2}
strokeLinecap="round"
{...props}
>
<path d="M5 12h14" style={{ strokeDasharray: '4 2' }} />
</svg>
);
export default Icon;A TypeScript icon
Input: <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#141413" stroke-width="2"><path d="M5 12h14"/></svg>, with TypeScript, icon mode and the name arrow-right:
import type { SVGProps } from 'react';
const ArrowRight = (props: SVGProps<SVGSVGElement>) => (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
width="1em"
height="1em"
{...props}
>
<path d="M5 12h14" />
</svg>
);
export default ArrowRight;The original width and height were replaced by 1em, and the color #141413 became currentColor.
Cleaning an exported file
An SVG with inkscape:version, an onload="alert(1)" handler, a <style> block and a <g inkscape:label="x" data-id="1" aria-hidden="true"/>, converted as "JSX only", gives:
<svg xmlns="http://www.w3.org/2000/svg">
<style>
{`.a{fill:red}`}
</style>
<g data-id="1" aria-hidden="true" />
</svg>The handler and the Inkscape attributes are gone, while data-id and aria-hidden stay.
Common mistakes
- Pasting something that is not SVG. Text without an
<svg>element is rejected with "There is no <svg> element in this text." - Unclosed tags. SVG is XML, so
<path>needs/>or a closing tag. The error shows where the parser stopped. - Expecting the icon to change color without icon mode. A hard-coded
fill="#141413"stays that color. Turn on icon mode or passfillthrough props. - Removing
viewBox. Without it the icon will not scale. The converter keeps it, so do not delete it afterwards. - Spreading props and then overriding.
{...props}is last on purpose, so props you pass override the defaults in the file. - Expecting scripts to survive.
<script>and handlers are removed on purpose; they are code, not drawing.
Frequently asked questions
Why does JSX use className and strokeWidth?
JSX attributes map to DOM property names, which are camelCase, and class is a reserved word in JavaScript. SVG attributes with a dash, such as stroke-width, become camelCase too.
Is my SVG uploaded anywhere?
No. The conversion runs in your browser, and the preview is drawn as an image, so scripts inside the file cannot run there.
What about the title and accessibility?
The <title> is kept, so a screen reader can name the icon. For decorative icons, add aria-hidden="true" through props.
Can I also use the SVG as a CSS background?
Yes, with SVG and image to CSS. If you need to convert inline styles by hand, the React style converter does that.