SVG to React Component: camelCase, Props and viewBox
Last updated: · 7 min read
An icon exported from Figma, Inkscape or Illustrator is XML. It carries attributes such as stroke-width, xlink:href and class, an XML declaration, comments and editor-specific namespaces. Paste it into a React component as it is and you get either a compile error or a console full of warnings, because attribute names in JSX follow JavaScript's rules. The SVG to JSX / React Component Converter renames the attributes, removes the editor leftovers and, if you ask for it, wraps the markup in a component that accepts props. This guide covers the conversion rules, the difference between using an SVG as a component and using it as an image, and three decisions the tool cannot make for you: colour, scaling and accessibility. The output in the example is the tool's real result.
Formula
Rules:
1. The SVG is parsed as XML: names are case-sensitive and the input must contain a single <svg> root element.
2. The XML declaration, DOCTYPE, comments, the <metadata> element and elements or attributes from editor namespaces (inkscape:…, sodipodi:…, xmlns:inkscape) are removed; xmlns is kept.
3. Hyphenated attributes are camel-cased: stroke-width → strokeWidth, fill-rule → fillRule, clip-path → clipPath. data-* and aria-* attributes stay as they are.
4. xlink:href → xlinkHref and xmlns:xlink → xmlnsXlink; class → className.
5. style="stroke-opacity: 0.8" becomes the object style={{ strokeOpacity: "0.8" }}.
6. Elements without content are self-closed: <circle … />.
7. With the "React component" option the markup is wrapped as const Name = (props) => ( … );, {...props} is added as the last attribute of the root <svg>, and an export default Name; line follows. The component name must start with a capital letter and contain only ASCII letters and digits; if it is empty, Icon is used.
Input (component name: ClockIcon):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Inkscape -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" inkscape:version="1.3">
<metadata>clock</metadata>
<circle class="ring" cx="12" cy="12" r="10"/>
<path d="M12 6v6l4 2" style="stroke-opacity: 0.8"/>
</svg>
Output (React component):
const ClockIcon = (props) => (
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...props}>
<circle className="ring" cx="12" cy="12" r="10" />
<path d="M12 6v6l4 2" style={{ strokeOpacity: "0.8" }} />
</svg>
);
export default ClockIcon;
Output (JSX only):
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
<circle className="ring" cx="12" cy="12" r="10" />
<path d="M12 6v6l4 2" style={{ strokeOpacity: "0.8" }} />
</svg>This is a renaming scheme rather than a formula. According to the React documentation, JSX attributes turn into keys of a JavaScript object and so cannot contain dashes; you write strokeWidth instead of stroke-width. Namespaced attributes are written without the colon as well: xlink:href becomes xlinkHref. The tool translates syntax only. It does not simplify paths, change colours, or add a viewBox or accessibility attributes. The output is indented with two spaces.
What the inputs mean
- SVG
- The SVG code including its <svg …> tag. A leading XML declaration, DOCTYPE and comments are allowed; the tool removes them. Up to 1,000,000 characters are processed.
- Output
- "JSX only" returns just the <svg> element; "React component (with props)" wraps the same markup in an arrow function that takes props and exports it as the default export.
- Component name
- The name used with the component option. It must begin with a capital letter (ClockIcon); React treats tags that start with a lower-case letter as HTML elements. Icon is used when the field is empty.
Worked example: Turning a clock icon from Inkscape into a component
- The first two lines of the input, the XML declaration and the comment <!-- Created with Inkscape -->, do not appear in the output.
- On the root <svg>, the attributes xmlns:inkscape and inkscape:version are removed; xmlns, width, height, viewBox, fill and stroke stay as they are.
- stroke-width="2" becomes strokeWidth="2" and stroke-linecap="round" becomes strokeLinecap="round".
- The <metadata>clock</metadata> element is removed. class="ring" becomes className="ring", and style="stroke-opacity: 0.8" is turned into the object style={{ strokeOpacity: "0.8" }}.
- With the component option, {...props} is appended to the root tag. Because it comes last, a value passed from outside overrides the earlier one: <ClockIcon width={32} height={32} /> draws the icon at 32 pixels.
- The "JSX only" option returns the same four lines of <svg> markup without the wrapping function and without {...props}.
Result: The component output is eight lines long: it starts with const ClockIcon = (props) => ( and ends with export default ClockIcon;. Because stroke="currentColor" is preserved, the icon takes the text colour of the element it sits in.
🖼️ SVG to JSX / React Component Converter
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
Embedding an SVG as a component and using it through <img> or a CSS background lead to different results. Inline SVG is part of the document: CSS can style it, currentColor follows the color property of the parent, and individual parts can be animated. In exchange, every icon is added to your JavaScript bundle and to the DOM, and it is not cached like a separate image file. For large illustrations that need no styling, <img> is often the better choice. Check yourself the three things the tool leaves alone. Colour: fixed colours such as fill="#000" do not follow the text colour; write currentColor if they should. Scaling: viewBox maps a rectangle in user space (min-x, min-y, width, height) onto the viewport; without a viewBox, changing width and height does not scale the drawing. Accessibility: give a decorative icon aria-hidden="true", and a meaningful one role="img" together with aria-label, or add a <title>; thanks to {...props} you can pass these where the icon is used. This is general information.
Common mistakes
- Pasting only the inner shapes. For input such as <circle r="1"/> the tool reports "The input must contain a single <svg> root element: paste the SVG code including its <svg …> tag."
- Naming the component clock-icon or starting it with a lower-case letter. The tool answers "Invalid component name: it must start with a capital letter and contain only ASCII letters and digits (example: Icon)."
- Trying to resize an SVG that has no viewBox through props. width and height change but the drawing does not scale; it is cropped or stays small in a corner. Keep the viewBox when exporting.
- Expecting an icon with fixed colours to follow the text colour. The tool does not turn fill="#000" into currentColor; that edit is yours to make.
- Rendering a component that relies on ids (gradients, clipPath, mask) several times on one page. The tool does not change id values, so the same id is repeated in the document and url(#a) references may resolve to the wrong definition.
- Relying on a <style> block inside the SVG. Once the SVG is inline, those rules apply to the whole page, and generic class names such as .cls-1 can collide with other icons.
Frequently asked questions
Why does stroke-width become strokeWidth?
The React documentation explains that JavaScript variable names cannot contain dashes, which is why many HTML and SVG attributes are written in camelCase in React. The documentation uses this very example: strokeWidth instead of stroke-width.
Do I still need the xmlns attribute in JSX?
Not for inline use: according to SVG 2, in the HTML syntax the namespace is supplied by the HTML parser. The tool keeps the attribute anyway and it does no harm. If you use the same SVG as a separate file or a data URI, xmlns is required.
How do I change the size and colour from outside?
With the component option, all props are passed to the root <svg>: <ClockIcon width={32} height={32} style={{ color: "tomato" }} />. Since {...props} comes after the SVG's own attributes, your value replaces the original; the colour changes only if the drawing uses currentColor.
Can I use the output with TypeScript?
The output is plain JavaScript. In a .tsx file you only need to type the props parameter: (props: React.SVGProps<SVGSVGElement>).
How do I make the icon known to screen readers?
For a meaningful icon, write <ClockIcon role="img" aria-label="Clock" /> or add a <title> to the SVG; SVG 2 requires the text of title and desc to be made available to accessibility APIs. For a decorative icon that sits next to visible text, use aria-hidden="true".
Is my SVG uploaded to a server?
No, the conversion runs in your browser.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.