How to use SVGs in your React App
Tags: react, webdev
Published: 2020-08-10 22:25:06

---
title: How to use SVGs in your React App
published: true
date: '2020-08-10T21:37:42.291Z'
tags: ['react', 'webdev']
canonical_url: https://maxrozen.com/how-to-use-svg-react-app/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/jbngv4950urldp9gz5zi.jpg
---

Trying to render an SVG in your React app, and getting errors? You’re not alone - it’s a relatively common issue.

There are two ways to do it, and both have tradeoffs.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#using-img-tags-and-passing-your-svgs-url)Using `<img>` tags, and passing your SVG’s URL

Here’s a basic example:

```
import React from 'react';
import logoSrc from './logo.svg';

const MyLogo = () => {
  return <img src={logoSrc} />;
};
```

The benefit of this approach is that your logo will not end up in your bundle, but rather exported as a static file when you run `yarn build` (assuming you’re using a standard webpack config, such as the one found in create-react-app).

This then gives you the option of aggressively caching icons that you know won’t change.

You would typically use this approach for larger company logos on your marketing site, or for illustrations in your app.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#creating-a-react-component-and-passing-props)Creating a React component, and passing props

The other option is to create a React component containing your SVG. Also known as “inlining” your SVG.

This done by pasting your raw `svg` markup into a new React component.

There are a few ways to achieve this:

- Manually, byremoving/replacing all HTML props with the React equivalent, and adding `{...props}` to the main `svg` element),
- CLI via [SVGR](https://github.com/gregberge/svgr) - a utility to automate this process
- Webpack config via [SVGR](https://react-svgr.com/docs/webpack/)

If you’re using create-react-app, it already has SVGR’s webpack config built-in, so you can already use your SVGs like React components:

```
import Star from './star.svg';
const App = () => (
  <div>
    <Star />
  </div>
);
```

Here’s what a manually created SVG-based React component looks like:

```
import React from 'react';

export const DeleteIcon = (props) => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    height="24px"
    viewBox="0 0 24 24"
    {...props}
  >
    <path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z" />
    <path d="M0 0h24v24H0z" fill="none" />
  </svg>
);
```

This approach lets you easily access props on your SVG icon. For example, changing the fill color:

```
<DeleteIcon fill="#fff" />
```

The downside being that your icons won’t be as easily cached, so I would use this approach for smaller icons, such as the [Material Design Icons](https://material.io/resources/icons/?style=baseline).

(This is an article posted to my blog at maxrozen.com. You can read it online by [clicking here](https://maxrozen.com/how-to-use-svg-react-app/).)