Does anyone know how to use an svg file in React? I am using Webpack and trying to use an svg file that I have saved. I have currently tried the following solutions, to no avail:
Yes, it can be, but it is not as simple as say adding an img element. I came across the following which explains how to incorporate SVG elements and also provides two alternatives.
I think I need to give it a try to that plugin. I USED SVG quite often with React but I always made my own components that render the SVG code as I need it. I suppose this plugin makes it better.
Yes, I’m with @Parmar93 on this. I think that’s the most straightforward approach, but as I said I never tried one of those webpack plugins, so maybe it’s not the best way to do it.
@jv88899 Have a look at my Pomodoro. I used SVG from line 514 onwards. Maybe it can be useful for you.
I’ve used https://github.com/gilbarbara/react-inlinesvg in the past and it worked well. The images were hosted elsewhere though, so I just passed in the path. Worked pretty easily out of the box. The advantage of this library is you can style the fill and target elements within the svg with css.
You could also try <img src={path} /> and host the svg on github.
Do you have a github repo or have this in an online editor so people here can have a look and help you out?