React
How to use SVG icons in React
Learn three ways to use SVG icons in React, style them with currentColor, and troubleshoot sizing, duplicate IDs, and bundle size.
Use a named React icon import for recurring interface controls. Use inline SVG when your component needs to modify an icon’s paths or colors, and use an SVG file when the artwork can keep its own styling. If your team already has an icon library, start there so icons share consistent geometry and weight.
Import an icon as a React component
Install Lucide for React with npm install lucide-react, then import the icon by name. Lucide renders an SVG that follows the surrounding text color by default.
import { Download } from 'lucide-react'
export function DownloadButton({ onDownload }) {
return (
<button type="button" onClick={onDownload}>
<Download size={18} aria-hidden="true" />
Download report
</button>
)
}
The visible text gives the button its accessible name. If you remove it, put aria-label="Download report" on the button and keep the icon decorative. See the React icon accessibility guide for toggle states and status messages.
Named imports also help bundlers retain only the icons a component uses. When building a small set of interchangeable icons, map supported names to specific imports. Importing an entire package namespace and indexing it with an arbitrary string can make it hard for a bundler to eliminate unused code. Confirm the result with a production build if you have a bundle budget. See Lucide’s React import guide.
In Next.js App Router, a simple SVG icon can render on the server. Put a button that calls a browser event handler inside a Client Component, and keep that boundary close to the interaction.
Inline an SVG when you need to style its paths
Keep the original viewBox, use JSX attribute names such as strokeWidth, and use currentColor for a monochrome icon that should inherit the surrounding text color.
export function CheckIcon() {
return (
<svg aria-hidden="true" width="20" height="20" viewBox="0 0 24 24"
fill="none" stroke="currentColor" strokeWidth="2">
<path d="m5 12 4 4L19 6" />
</svg>
)
}
Only inline trusted SVG markup. If the graphic conveys information on its own, provide an accessible name or put the meaning in adjacent text.
Keep the viewBox and choose a display size
The viewBox describes the drawing’s internal coordinate space. The display width and height determine how much room the icon takes in your layout. A 24-unit drawing can display at 16 px without rewriting its path coordinates. Preserve the original viewBox when copying markup, including any non-zero origin. A missing viewBox is a common reason a resized icon looks clipped or fails to scale.
If a stroke icon should follow its parent’s text color, use stroke="currentColor" and set fill="none" where appropriate. For a single-color filled icon, try fill="currentColor". Keep intentional color values in artwork that needs multiple colors. Compare the result in light and dark themes.
Give repeated gradients and masks unique IDs
Inline SVG definitions share the page’s ID space. If a component always generates id="gradient", two copies can end up pointing at the same definition. Generate an ID per instance and use the same value in the reference:
import { useId } from 'react'
export function AccentDot() {
const fillId = useId()
return (
<svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20">
<defs>
<linearGradient id={fillId}>
<stop stopColor="#2563eb" />
<stop offset="1" stopColor="#9333ea" />
</linearGradient>
</defs>
<circle cx="10" cy="10" r="8" fill={`url(#${fillId})`} />
</svg>
)
}
This also applies to clipPath, mask, and labels referenced by aria-labelledby. See React’s useId reference.
Use an SVG file for fixed artwork
For a static logo, put the SVG in your public assets and use an image element:
<img src="/brand-mark.svg" alt="Acme" width="32" height="32" />
If the same name already appears beside the image, use alt="". Set both dimensions to reserve layout space. When you load an SVG as an image, CSS in the parent document can style the image box but cannot set the fill of paths inside it. Importing an .svg file as a JSX component requires build tooling that supports that transform.
Build a reusable icon component for a report dashboard
Suppose a dashboard needs a file icon beside report names, a download icon inside a button, and a clock icon beside the last-updated date. All three icons can inherit the color of their surrounding interface. Define a short list of supported symbols and use a small component that exposes the sizes your design system already uses.
import { Clock3, Download, FileText } from 'lucide-react'
const reportIcons = {
file: FileText,
download: Download,
updated: Clock3,
}
type ReportIconProps = {
name: keyof typeof reportIcons
size?: 16 | 20 | 24
}
export function ReportIcon({ name, size = 20 }: ReportIconProps) {
const Icon = reportIcons[name]
return <Icon size={size} aria-hidden="true" focusable="false" />
}
Place the component beside the text it illustrates, for example <ReportIcon name="updated" size={16} /> Last updated today. To build a download control, put the icon and the label inside the same button or link. An ordinary link is appropriate when the action follows a download URL. Use a button inside a Client Component when the action needs browser-side logic, such as generating a file from the current filters.
The wrapper deliberately represents decorative UI icons. For a meaningful diagram or graphic, use an image with alternative text or a separately named inline SVG. Keeping those uses explicit makes it easier to review the accessibility of each component and prevents a default prop from hiding something important.
Keep alignment and target size in the parent control
If an icon looks off-center beside a label, start with the layout. An inline SVG can sit on the text baseline and leave a small gap underneath. A button with display: inline-flex, align-items: center, and a gap aligns the artwork and text as children of the same control:
.report-action {
display: inline-flex;
align-items: center;
gap: 0.5rem;
min-block-size: 2.75rem;
padding-inline: 0.75rem;
}
.report-action svg {
display: block;
flex: none;
}
.report-action:focus-visible {
outline: 2px solid currentColor;
outline-offset: 3px;
}
The 44 px example is a design choice for this button. Set your product’s target size independently of the drawing and check it at the narrowest supported layout. If text wraps, flex: none keeps the icon from shrinking into a different size. Keep the default focus indicator or provide a clearly visible replacement on every theme.
Diagnose common SVG rendering problems
The icon takes the wrong color
Inspect the rendered SVG and its paths in DevTools. A path can declare its own fill or stroke even if the SVG element inherits the correct color. For a monochrome outline, find the explicit stroke value and change it to currentColor. For multicolor artwork, separate the paths that should follow the theme from the colors that are part of the artwork. If the element is an image, adjust the source SVG or render trusted inline markup to control individual paths.
The edge is clipped or the stroke feels too heavy
Compare the exported viewBox with the original. A stroke can extend beyond the visible shape’s coordinates, so a tightly cropped canvas can cut off the outer half of an edge. Test the icon at the sizes your actual interface uses. Going from 24 px to 16 px scales the drawing and usually scales its stroke as well. If it becomes hard to distinguish from nearby icons, try a simpler symbol or a library variant intended for small sizes. For details about how coordinates map to the display box, see the SVG viewBox reference.
The page ships more icon code than expected
Run a production build, then use the browser Network panel to inspect the JavaScript requests for the affected page. Compare the same page before and after the change with the browser cache disabled. A broad icon registry can keep every component in the registry reachable, even with named imports, because the browser must handle every supported key. Use a small registry for each feature. A public icon picker with thousands of possibilities needs a separate asset-loading strategy from a toolbar with six fixed actions.
Check icons before shipping
- Render several icons at their real UI size. Look for uneven whitespace, clipped strokes, and hard-to-see states.
- Inspect the computed
color,fill, andstrokeof any icon that ignores your theme. - Render two copies of any SVG that uses gradients or clipping, and check both after a page refresh.
- Confirm what a keyboard user can focus and what a screen reader calls each icon button.
- Record the source library and any required license notice for files copied into your repository.
Compare export options in the React icon guide or find a specific icon with IconSearch.