question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider importing SVGs as React components

See original GitHub issue

My own path of using SVGs with webpack has settled me comfortably with loading .svg files as react components via https://github.com/jhamlet/svg-react-loader (svg-react-loader@next as of time of writing). This allows you to treat an SVG just like any other React component, and renders out inline SVGs. For example:

const MySvg = require('./mySvg.svg')

const MyApp = () =>
  <div>
      // some components!
     <MySvg /> // could also pass fill='#ccc' or style={styleObj}
  </div>

// renders
/*
<div>
  <svg ...> ...etc </svg>
</div>
*/

Inline SVGs offer the most amount of flexibility in terms of styling, animation, and manipulating props. SVGs in the browser are now more supported than ever. They are quickly replacing icon fonts as the preferred way to do icons in your app.

I’d enjoy hearing the community’s thoughts on this.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:84
  • Comments:78 (32 by maintainers)

github_iconTop GitHub Comments

97reactions
ericdfieldscommented, Jan 13, 2017

This video convinced me that the way we should be using SVG is inline. This table, showing the features and benefits of implementing SVG using the three available techniques, cinched it for me:

screen shot 2017-01-13 at 10 45 27 am

An SVG is different from all other assets (image, sound, video, etc) in that isn’t a binary blob, and so can be more easily manipulated in our app. An SVG has more in common with a stateless react component than a JPEG.

You can still do this:

import Logo from './logo.svg';
...
<Logo className='App-logo' ariaLabel='logo' />
...

Then again, I suppose I haven’t thought through the scenario of using an SVG asset in CSS… 😬

75reactions
kauffecupcommented, Jan 27, 2017

as a workaround you can do something like:

import React from 'react';
export default () =>
  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
    <path d="M2.001 9.352c0 1.873.849 2.943 1.683 3.943.031"/>
  </svg>

then you can take in props and use those to adjust the fill

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use SVGs in React - LogRocket Blog
SVGs can be imported and used directly as a React component in your React code. The image is not loaded as a separate...
Read more >
How to use SVGs in React | Sanity.io guide
Import it as a component via bundler magic (SVGR); Include it directly as JSX. Let's quickly go over each of them. Use SVGs...
Read more >
How to display svg icons(.svg files) in UI using React ...
import { ReactComponent as YourSvg } from './your-svg.svg';. And then use it just like you would normally use a component: const App =...
Read more >
The Best Way to Import SVGs in React - Better Programming
In this article, I will show the two methods to import SVG assets into React components. I like to call them the in-source...
Read more >
How to Import SVGs in a React and Vite app - freeCodeCamp
Importing SVGs using the image tag is one of the easiest ways to use an SVG. If you initialize your app using CRA...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found