Skip to content
Tutorials Center
My learning

Topics

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.

JSX
<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

A ring driven by state
Icons that follow the text color

Syntax

JSX
<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. Use fill="currentColor" or stroke="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 use role="img" with aria-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>, and xmlns is 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 path strings 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, use useId() so the ids do not collide.
  • Importing files. Bundlers can turn .svg files 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 requestAnimationFrame with a ref instead of a state update per frame.