MEDIA GUIDES / Front-End Development

How to Use SVGs in React: A Simple Guide

SVGs are a type of vector image format that uses XML to describe 2D graphics and can include animations and interactive elements. They often enter a React project quietly, initially appearing as icons dropped into a folder or a logo added to a header.

Due to their unique features, they are the preferred choice in modern interfaces. SVG format images are used as icons, logos, charts, loaders, and small illustrations. It is a preferred option to plain image files like PNG or JPG for preserving quality.

Key takeaways:

  • SVGs are vector graphics made from shapes and paths, so they scale perfectly at any size without losing quality. They work especially well in modern UI and React projects because they can be styled, animated, and reused as components that respond to themes and user interactions.
  • React supports several ways to use SVGs, including img tags, inline JSX, and reusable SVG components. Using img tags is best for static graphics, inline SVGs allow more control and interaction, and component-based SVGs offer the most flexibility and reuse for scalable design systems.

In this article:

SVG Basics: What Are They and Why Use Them

Unlike regular image formats like JPEG or PNG, SVGs are built from paths and shapes described with XML-based vector graphics. This means they scale cleanly at any size. A small icon and a full-screen illustration can use the same SVG file without losing clarity.

SVGs also support styling and interaction. CSS colour change, icon animation, and interaction are also great reasons why SVGs are a great choice. When it comes to flexibility, SVGs are a strong choice in UI elements that need to adapt to theme changes or user actions.

In React projects, SVGs shine because they fit into component-based thinking. An icon can become a reusable component. A chart can respond to state changes. A logo can switch colors based on mode or context.

How to Use SVGs in React

Before looking at the different methods, it helps to set up a simple React project.

If you are using a modern tool like Vite or Create React App, SVG support already exists. You can import files, reference them as assets, or turn them into components depending on your needs.

In React, there are three primary methods for using SVGs, and each of them have their peculiarities when it comes to control and customization.

1. Using SVG inside an img tag

This is the simplest approach. You treat the SVG like a normal image, import the file and pass it to an img tag. This works well for static images such as logos or decorative icons.

The browser handles loading and caching. The downside is limited control–you cannot easily change internal colors or animate paths without editing the SVG file itself. Use this method when the SVG doesn’t need to react to state or props.

2. Using SVG inline in JSX

Inline SVG gives you more control, as you directly embed the SVG markup into your component. This allows you to modify fills, strokes, and sizes using props or CSS. You can attach event handlers and animate parts of the SVG.

This approach works well for small, one-off graphics, such as icons that require dynamic color changes or interaction. The tradeoff is that inline SVG can add noise to your component file if the SVG is complex.

A common pattern is to clean up the SVG first. Remove unnecessary metadata and group elements clearly before pasting them into JSX.

3. Turning SVGs into reusable components

This is often the best long-term approach. Many React setups allow you to import an SVG as a component.

The SVG becomes a function that returns JSX; so you can pass props like size, color, or className. This method allows for full customization and keeps your components clean and reusable. You can store all icons in a single folder and treat them like UI building blocks.

For example, an IconCheck component can be reused across buttons, forms, and alerts. If you later change the design, you update one file, and the changes apply across your application. This pattern fits well with design systems and shared component libraries.

Performance Tips and Common Pitfalls

SVGs are light, but they can still cause issues if used poorly. Here are four tips to ensure optimal performance.

  1. Large SVG files with thousands of paths can slow down rendering. This often happens with exported illustrations that were never optimized. Tools like SVG optimizers help reduce file size by removing unused data.
  2. Avoid inline SVG for very complex artwork unless you need fine control. Inline SVG lives in the DOM and adds to layout and paint work. For large decorative graphics, using an image tag or a background image can be better.
  3. Be careful with accessibility. SVGs used as icons should include proper labels or titles when they convey meaning. Decorative SVGs should be hidden from screen readers to avoid noise.
  4. Naming conflicts are another pitfall. Inline SVG IDs can clash when multiple instances render on the same page. This can break gradients or clip paths. Cleaning up IDs or using unique prefixes helps prevent this issue.

Manage SVGs At Scale with Cloudinary

As projects grow, managing SVG assets becomes harder. Teams often deal with hundreds of icons across multiple products. Keeping them optimized and consistent takes time.

This is where Cloudinary helps.

Cloudinary not only serves as a central bank for SVGs, but it also serves as a dispatch tool, delivering them efficiently. SVGs can be transformed in seconds, whether you need to manipulate size, alter colors, or apply effects; Cloudinary does that without editing the source file. This makes it optimal when the same icon needs different sizes or themes across an app.

Cloudinary also handles caching and delivery through a global network. SVGs load quickly no matter where users are located. For React apps that serve a global audience, this improves consistency and performance.

Wrapping Up

SVGs fit naturally into React because they both favor flexibility and reuse. You can start simple with image tags and move toward inline or component-based SVGs as your needs grow. Each approach has a place depending on control performance and scale.

Clean SVG files, reusable components, and thoughtful delivery make a huge difference as projects mature, but it all starts with choosing the right method.

Streamline your media workflow and save time with Cloudinary’s automated cloud services. Sign up for free today!

Frequently Asked Questions

Are inline SVGs bad for performance?

Inline SVGs are fine for small and medium icons. Problems appear when large illustrations with many paths are rendered in-line. They add weight to the DOM. For complex artwork, it is better to load SVGs as images or serve them through a media service.

Should I optimize SVG files before using them?

Yes. Many SVGs exported from design tools contain extra data that increases file size. Optimizing them removes unnecessary metadata and keeps rendering fast, especially when SVGs appear many times on a page.

Can SVG IDs cause bugs in React?

Yes, when the same SVG is rendered multiple times. IDs inside the SVG can clash and break gradients or masks. Cleaning IDs or making them unique avoids this issue.

QUICK TIPS
Jen Looper
Cloudinary Logo Jen Looper

In my experience, here are tips that can help you better manage SVGs in React projects at scale:

  1. Build an “icon contract” and enforce it
    Standardize props (size, title, decorative, color, className, style) and default behaviors (e.g., size=16, decorative=true). A consistent contract prevents every team from inventing a slightly different icon API.
  2. Make currentColor your default, not a special case
    Convert fills/strokes in your icon set to fill="currentColor" (and stroke="currentColor" where appropriate). Then your icons inherit text color automatically, making them theme-ready and eliminating a lot of one-off prop wiring.
  3. Ship two SVG variants: “icon” and “illustration”
    Icons should be tiny DOM footprints (few paths, no filters). Illustrations can be heavier but should default to <img> (or external asset) unless you truly need DOM-level interaction. This split prevents accidental inline mega-SVGs creeping into UI.
  4. Automatically prefix IDs during the build
    Don’t rely on humans to fix id="clip0" collisions. Add a build step (SVGR/SVGO config) that prefixes all IDs with a hash or filename and rewrites url(#...) references. This permanently eliminates gradient/mask/clip-path bugs in repeated renders.
  5. Treat accessibility as a first-class prop, not an afterthought
    Implement a pattern where decorative toggles aria-hidden="true" and removes <title>, while meaningful icons require a title and set role="img". This keeps screen readers clean without making every usage verbose.
  6. Avoid “CSS fights” by exposing a single styling hook
    Pick one: className + currentColor, or style props, or CSS variables (my favorite: --icon-color, --icon-stroke). Multiple competing styling paths lead to hard-to-debug overrides and inconsistent theming.
  7. Use viewBox discipline to prevent “mystery padding”
    Normalize viewBoxes so the artwork is snug (no extra whitespace) and consistent across the set (e.g., 24×24). Inconsistent viewBoxes cause alignment issues that developers waste hours “fixing” with random margins.
  8. If you animate, animate transforms—not path data
    Path morphing is expensive and brittle across browsers. Prefer animating transform, opacity, or stroke dash properties. You’ll get smoother motion and far fewer rendering quirks, especially on lower-end devices.
  9. Create an icon “lint” checklist for designers exporting from Figma/Sketch
    Ban filters unless justified, expand strokes when needed, remove hidden layers, convert text to outlines, and ensure no embedded rasters. Most SVG pain in React comes from messy exports, not React itself.
  10. Instrument and cap SVG complexity in CI
    Add a CI check that fails if an icon exceeds thresholds (file size, path count, node count). This stops one overly-detailed export from silently degrading performance across every button/menu where it’s reused.
Last updated: Feb 11, 2026