How to display svg icons(.svg files) in UI using React Component?

Kamaraju picture Kamaraju · Feb 17, 2017 · Viewed 250.7k times · Source

I have seen a lot of libraries for svg on react but none gave me how to import an svg file in the react component. I have seen code which talk about bring the svg code into react rather than using the .svg icon as image and show it in the UI.

Please let me know if there are ways to embed the icon.

Answer

Tsuni picture Tsuni · Oct 2, 2018

If you use create-react-app 2.0 you can now do it like this:

import { ReactComponent as YourSvg } from './your-svg.svg';

And then use it just like you would normally use a component:

const App = () => (
 <div>
   <YourSvg />
 </div>
);