React Reference ยท Built-in DOM Components
SVG elements in React
Reference for SVG in React: write inline svg with camelCase attributes like strokeWidth, size with viewBox, color with currentColor and drive shapes from state.
React can render any SVG element inline: <svg>, <path>, <circle>, <rect>, <g>, <text>, <defs>, <linearGradient> and the rest. They are ordinary JSX elements, so attributes can come from props and state, and shapes can respond to events. Inline SVG is the usual way to build icons, charts and small illustrations without extra image requests.
The rule to remember: attribute names are camelCase in JSX where the SVG attribute has a hyphen. stroke-width becomes strokeWidth, fill-opacity becomes fillOpacity, stroke-linecap becomes strokeLinecap, clip-path becomes clipPath. Attributes that are already one word (fill, stroke, cx, d, points) and viewBox keep their spelling. The class attribute is className.
<svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"> <path d="M5 12h14" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /> </svg>
Try it
Syntax
<svg viewBox="minX minY width height" width={n} height={n} xmlns="http://www.w3.org/2000/svg"> <circle cx={n} cy={n} r={n} fill="#hex" strokeWidth={n} /> </svg>
| Attribute in SVG | In JSX |
|---|---|
stroke-width, stroke-linecap, stroke-linejoin, stroke-dasharray, stroke-dashoffset, stroke-opacity |
strokeWidth, strokeLinecap, strokeLinejoin, strokeDasharray, strokeDashoffset, strokeOpacity |
fill-opacity, fill-rule, clip-rule |
fillOpacity, fillRule, clipRule |
text-anchor, font-size, font-family, font-weight |
textAnchor, fontSize, fontFamily, fontWeight |
clip-path, stop-color, stop-opacity |
clipPath, stopColor, stopOpacity |
xlink:href |
href (modern) or xlinkHref |
class |
className |
viewBox, preserveAspectRatio, d, cx, cy, r, x, y, points, transform |
unchanged |
Returns: SVG DOM elements (SVGSVGElement, SVGPathElement, ...).
Edge cases: numbers are fine for geometry: r={40} becomes r="40". The style prop works on SVG elements and takes an object, but presentation attributes are usually clearer. Unknown hyphenated names such as stroke-width still render, but React warns that the name is invalid and suggests the camelCase form.
Usage notes
- Always set
viewBox. It defines the drawing's coordinate system, so the image scales with CSS width and height. Without it the shapes keep their pixel size. currentColor. Usefill="currentColor"orstroke="currentColor"in icons so they follow the text color and respond to dark mode.- Accessibility. For decorative icons add
aria-hidden="true". For meaningful graphics userole="img"witharia-label, or put a<title>as the first child. The<title>inside an<svg>is not hoisted to the document head. - Namespace is automatic. React creates SVG elements in the SVG namespace when they are inside
<svg>, andxmlnsis not needed for inline use in HTML. foreignObject. HTML placed inside<foreignObject>is created as HTML again, so you can embed a form or text block in a diagram.- Performance. Thousands of SVG nodes are slower than a canvas. Memoize static parts of big charts and avoid re-creating
pathstrings on every render when the data has not changed. - Unique ids. Gradients, masks and clip paths are referenced by
id. If a component renders several times, useuseId()so the ids do not collide. - Importing files. Bundlers can turn
.svgfiles into components or URLs. That is a build-tool feature and not part of React. - Animation. CSS animations work on SVG shapes. For frame-by-frame animation from state, prefer
requestAnimationFramewith a ref instead of a state update per frame.
Related
<img>,useId,<div>and<span>- Lessons: JSX in depth, Lists and keys