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.

Dynamically import svgs

See original GitHub issue

So 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:open
  • Created 5 years ago
  • Reactions:10
  • Comments:7

github_iconTop GitHub Comments

1reaction
mick-fellercommented, May 20, 2022

@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.

config.module.rules.push({
    test: /\.svg?$/,
    oneOf: [
      {
        use: [
          {
            loader: '@svgr/webpack',
            options: {
              prettier: false,
              svgo: true,
              svgoConfig: {
                removeViewBox: false
              },
              titleProp: true,
            },
          },
        ]
      }
    ]
  })

And then in our code we actually do this to generate an object of all our SVG’s in a folder:

const getSVGS = () => {
  const context = require.context('../../icons', true, /\.svg$/);
  const obj = {};
  context.keys().forEach((key) => {
    obj[key] = context(key).default;
  });
  return obj;
};
0reactions
jp1987commented, May 20, 2022

@yairEO @mick-feller did you ever fix this?

I’m trying to import inline:

import(`../Icons/${name}.svg`).then(icon => {
   console.log(icon);
})

Which returns the module.

Webpack config:

{
  test: /\.svg$/,
  use: ['babel-loader']
}

And running version 2.0.1

Read more comments on GitHub >

github_iconTop 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 >

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