简体   繁体   English

ReactJS SVG 图标加载失败并带有 svgr 以更改 SVG 填充动态

[英]ReactJS SVG icon load fails with svgr in order to change SVG fill dinamically

I've been trying to use SVG in ReactJS for the past 2 hours but nothing seems to be working.在过去的 2 个小时里,我一直在尝试在 ReactJS 中使用 SVG ,但似乎没有任何效果。

I'm using:我在用着:

"react": "^17.0.2",
"react-dom": "^17.0.2",

"devDependencies": {
        "@svgr/webpack": "^6.0.0",
        "react-svg-loader": "^3.0.3",

Using create-react-app使用 create-react-app

I call the icon as:我将图标称为:

import { ReactComponent as Logo } from '../../icons/logo.svg';
...
<Logo fill="white" />

And the error i get is:我得到的错误是:

./src/icons/logo.svg (./node_modules/@svgr/webpack/dist?-svgo,+titleProp,+ref!./src/icons/logo.svg)
TypeError: this.getOptions is not a function.

If i use it in an "img" tag, the icon shows, but I need to dinamically change the fill of the SVG.如果我在“img”标签中使用它,图标会显示,但我需要动态更改 SVG 的填充。 If there is a way on how i can load an svg and change the fill dinamically, please help and thank you.如果有办法让我加载 svg 并动态更改填充,请帮助并谢谢。

Try to import it as a variable, and then include that variable in an img tag in the src attribute as shown here.尝试将其作为变量导入,然后将该变量包含在 src 属性的 img 标记中,如此处所示。

 import Logo from '../../icons/logo.svg'; <img src={Logo} alt="logo" />

or add your svg as tag element and change the fill using css或添加您的 svg 作为标签元素并使用 css 更改填充

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM