SVG to JSX Converter
Convert SVG markup to JSX or a typed React component.
About SVG to JSX Converter
An SVG to JSX converter that rewrites attributes and inline styles for React and can wrap the result in a typed component that spreads its props.
How to use
- Paste SVG markup or load an
.svgfile. The JSX updates as you type. - Leave React component on to get a function component, and give it a name. With no name it takes the loaded file's name, or
Icon. - Spread props adds
{...props}to the<svg>element; TypeScript props types them asSVGProps<SVGSVGElement>. Keep comments turns SVG comments into JSX comments. - Copy the code or download it as
.jsxor.tsx.
What it does
- Renames attributes to the props React expects:
stroke-widthtostrokeWidth,classtoclassName,xlink:hreftoxlinkHref,xml:spacetoxmlSpace.data-andaria-attributes keep their hyphens, andviewBoxand other camelCase attributes keep their case. - Turns a
stylestring into an object:style="stop-opacity:1"becomesstyle={{ stopOpacity: 1 }}. Numbers stay strings unless React treats the property as unitless. - Self-closes empty elements, drops the XML declaration and doctype, and puts the contents of a
<style>element in a template literal. - Leaves out attributes in an editor's namespace, such as
inkscape:labelandsodipodi:docname, because JSX has no way to write them. They are listed under the output. - The spread comes last on the
<svg>, so a prop you pass, such aswidthorclassName, replaces the one in the file. - It does not optimise the drawing (run the SVG Optimizer first), replace colours with
currentColor, change sizes or target React Native. The markup is read by the browser's HTML parser, so a broken file is repaired the way a browser would repair it, not reported. Only the first<svg>in the input is converted.
Example
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" class="icon">
<path d="M5 12h14" stroke-linecap="round" style="stroke: currentColor"/>
</svg>
→
import type { SVGProps } from 'react';
export default function ArrowRight(props: SVGProps<SVGSVGElement>) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
strokeWidth="2"
className="icon"
{...props}
>
<path
d="M5 12h14"
strokeLinecap="round"
style={{ stroke: 'currentColor' }}
/>
</svg>
);
}
Privacy
The conversion runs in the browser. The SVG is parsed into an inert document, so nothing in it runs or loads. No request is made, and nothing is stored.
Frequently asked questions
- How do I use an SVG in React?
- There are three common ways: point an <img> at the file, let a bundler plugin such as SVGR import it as a component, or paste the markup into a component as JSX. Inline JSX is the one that lets you style the drawing with CSS and props, and it needs the attribute names converted first.
- Why does React warn about stroke-width or class in my SVG?
- JSX props follow JavaScript property names, so hyphenated SVG attributes are written in camelCase (strokeWidth, fillRule, clipPath) and class is written className. React still renders the hyphenated forms but logs an invalid DOM property warning for each.
- What is SVGProps<SVGSVGElement>?
- It is the type from the react package that describes every prop an <svg> element accepts, such as className, width, fill and onClick. Typing an icon component with it lets callers pass any of them through a spread.
- Should I still write xlink:href in an SVG?
- Not in new files. SVG 2 replaced xlink:href with plain href, and current browsers support it. Older files still use the xlink form, which React accepts as xlinkHref; this converter keeps whichever one the file has.