在React中如何导入依赖于prop值的svg组件

时间:2020-04-29 05:28:16

标签: reactjs svg import

我在node_modules中有一个库,其中包含50个svg。我渲染了一些显示svg之一的组件。它具有道具svgName,我想动态导入必要的svg。

// normally svg can be importted in this way
import svg1 from 'lib/1.svg';

const MyComponent = ({svgName}) => {
  // I want something like this
  const svg1 = import(`lib/${svgName}.svg`);

  return (
    // this component render svg in circle
    <Icon
      src={svg1}
    />
  );
}

我该怎么做?

0 个答案:

没有答案