Consider importing SVGs as React components
See original GitHub issueMy 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:
- Created 7 years ago
- Reactions:84
- Comments:78 (32 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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:
Then again, I suppose I haven’t thought through the scenario of using an SVG asset in CSS… 😬
as a workaround you can do something like:
then you can take in props and use those to adjust the fill