Dynamically import svgs
See original GitHub issueSo we have a library with all our svg icons in it.
I’m looking for a way to import all these svgs dynamically and display them in our styleguide (storybook) to give a visual representation of which icons we have.
I was trying something like this but no luck:
const reqSvgs = require.context('./svgdir', true, /\.svg$/);
reqSvgs.keys().map((filename) => {
return (
<div className="icon">
{reqSvgs(filename)}
</div>
);
})
But no luck, anyone any idea how i can make this work?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:7
Top Results From Across the Web
How to dynamically import SVG and render it inline
I want to dynamically import that svg based on the name passed to the function. It looks like this: import React from 'react'; ......
Read more >React, dynamically importing SVG's | by Eric Khoury | Medium
Import all SVG's from a folder at runtime, i.e based on props; Render the SVG as a React component, as in <MySvg />....
Read more >Using React.lazy() to dynamically import svg files as ... - Reddit
My attempt in making that simpler is the following component with all svg files in a different folder: import React, { Suspense }...
Read more >How can we dynamically import svg files? · Issue #19 - GitHub
Put in assets/icons all the svg that you will use. Create a file index.js and import and export the svg. // src/assets/icons/index.js import...
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 Free
Top 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
@jp1987 we actually moved away from this plugin and went with @svgr/webpack, the webpack piece is what we setup in storybook, but you can obviously convert this to regular webpack.
And then in our code we actually do this to generate an object of all our SVG’s in a folder:
@yairEO @mick-feller did you ever fix this?
I’m trying to import inline:
Which returns the module.
Webpack config:
And running version
2.0.1