Turning an SVG into a React component: the attribute rules that actually matter
An SVG pasted into a React component usually works at first and then produces a console full of warnings. The reason is that JSX is not HTML with a different file extension: it maps attributes to DOM properties. class is ignored because the property is className, stroke-width is unknown because the property is strokeWidth, and xmlns:xlink has no JSX meaning at all.
The mapping rule is mechanical. Hyphenated SVG presentation attributes become camelCase: stroke-linecap to strokeLinecap, fill-rule to fillRule, clip-path to clipPath, stop-color to stopColor. xlink:href becomes xlinkHref in React versions that still use it, although modern React accepts plain href and that is shorter.
| SVG attribute | JSX attribute | Note |
|---|---|---|
| class | className | Required — class is ignored |
| stroke-width | strokeWidth | All hyphenated attributes follow this rule |
| fill-rule | fillRule | Same for clip-rule, clipPath |
| xlink:href | xlinkHref or href | href is preferred in React 17+ |
| xmlns | — | Drop it for inline components |
| style="fill:red" | style={{fill: 'red'}} | A string works but an object is idiomatic |
| tabindex | tabIndex | Lower-case DOM attributes need camelCase too |
Drop what the build does not need
Remove xmlns, xmlns:xlink and version from the root. None of them affects rendering in JSX, and the namespace declarations are the longest attributes in most files. Keep viewBox: it is the coordinate system the drawing is written in, and it is what makes the component scale. If the original has width and height, replace them with a size prop rather than leaving fixed pixels.
Be careful with id. A component rendered twice on one page creates two elements with the same id, and every url(#gradient) in both instances resolves to the first one. If the icon uses gradients or masks, either accept that both instances share the definition or generate a unique id per instance with useId. A plain stroke icon with no references is safe.
A component that behaves like a component
The smallest useful wrapper spreads the rest props onto the svg element, so a caller can add aria-label, className, onClick or a ref without editing the component. Put {...props} after the attributes you set so a caller can override them, and keep the viewBox before it because overriding a viewBox is rarely intentional but still possible.
For TypeScript, type the props as SVGProps<SVGSVGElement> and add size?: number. That gives autocomplete for every SVG attribute and rejects typos such as strokeWidthh. Destructure size = 24 with a default so the component renders without props, and pass the remaining props through unchanged.
If the component will be used as a decorative icon next to text, add aria-hidden="true" and focusable="false". If it carries meaning, give it a title or an aria-label instead. A component cannot decide which case applies, so expose the choice through props and document the default.
A component library also needs a rule for sizing: a size prop for icons, a viewBox for illustrations, and no fixed width in the markup. When every component follows it, a page can change icon sizes with one CSS rule instead of editing each file.
Sources
Questions
Why does class become className in JSX?
JSX is not HTML. The DOM property for a CSS class is className, and React applies attributes through properties, so class is ignored with a warning. SVG elements use the same rule: class becomes className and style may be an object.
Do I need the xmlns attribute in a React component?
No. JSX creates SVG elements in the SVG namespace automatically, and xmlns on an inline svg is redundant. It is only needed when the markup is serialised to a standalone .svg file, so a component generator should drop it.
How should I size an icon component?
Give the component a size prop with a default and set width and height from it, while keeping the viewBox in the markup. Then a caller can write
Open the React and TypeScript tabs in the optimizer to get the same drawing as a ready component.
Open the optimizerWritten against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.
Back to the guides