Flexible SVG components in React

TL;DR: Using SVGs inside of React components allows us to control their size and color and SVGR is the right tool to create and maintain many SVG components at once.

While getting started with a new project, a colleague asked me about a good way to include icons into a next.js based React application.

It’s been quite a while since I implemented an icon system in one of my previous projects, so I quickly opened the source files of that project to refresh my memories. In hindsight, I felt that my approach worked quite well for what we were doing and I decided to share my insights in this blog post. Let’s dive in!

Getting SVGs into our projects

Using SVGs on the web isn’t hard, the format is supported by all major browsers since IE9. Of course, there are multiple ways to include them into our projects, like using them in image tags, as background-images or directly inline.

While all approaches are easy, the latter offers the opportunity to create self-contained, on-the-fly-customizable icon components, so I wanted to make use of that.

Suppose we have an icon SVG depicting an airplane, that we want to use in various colors and sizes throughout our project. The SVG code might look something cryptic like this:

So far so good, your typical .svg file. What’s of notice is that there are properties for the viewbox and the path’s fill. While the viewbox specifies the area that contains the path, there are more possible properties on the root svg tag, specifically width and height as well as fill .

The fill property, for example, can be specified as

and reused inside of the path tag (or tags) for the specific paths like so.

Lord of the Props

This is a perfect opportunity for controlling these properties via props thrown into a React component that renders the svg tag. We can now easily define our Airport stateless functional component as

and use it via:

It’s over, it’s done! We got ourselves a nice, controllable little component, where we can specify height , width and fill to cover our various use cases. We are now true rulers of the SVG world, aren’t we? But what if we had to manage hundreds of icons in a large-scale application? Wouldn’t exporting, optimizing and setting up components take hours — and what if icons change? Oh, the mess!

SVGR to the rescue

There surely must be a package that handles all this — and SVGR is the one. After installing the dependency via npm install svgr in our project root, we can easily create React components off our many .svg files automatically.

Let’s add a new npm script to our package.json file and call it icons :

Basically, this command takes the path ./src/icons/svg as the location to our /svg directory containing all of our icons and outputs the icons as components into the ./src/icons/js directory. The --replace-attr-value is the magical part of the script, where we replace thefillattribute value of"#FFF” with "fill" as explained above.

After running this command via npm run icons , all our components are created inside of the /js directory. Suppose we had our airport.svg file inside of /svg , we now have the corresponding Airport.js file containing the code above — by merely lifting a finger. Awesome!

Taking this further

We might want to access the icons by a String value, or we might want to color multiple path inside our components differently, which is all possible. To keep this post concise, I will cover these aspects in the future. Stay tuned!

I am a Student seeking my Master's Degree and currently working as a Fullstack Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store