admin管理员组文章数量:1404577
I'm trying to fetch SVG images dynamically in my react ponent, but so far I'm getting this error Uncaught (in promise) TypeError: Failed to fetch dynamically imported module. This is my code sample
import React from 'react';
import { CustomSvgIcon } from '../../util/CustomSvgIcon';
const Icon = ({ iconName }: IconProps) => {
const [menuIcon, setIcon] = React.useState<string | null>(null);
React.useEffect(() => {
if (iconName) {
import(`../../../assets/svg/${iconName}.svg`)
.then((bla) => setIcon(bla.default))
.catch((error) => console.log(error));
}
}, []);
return (
<CustomSvgIcon>
{menuIcon ? menuIcon : null}
</CustomSvgIcon>
);
};
I'm trying to fetch SVG images dynamically in my react ponent, but so far I'm getting this error Uncaught (in promise) TypeError: Failed to fetch dynamically imported module. This is my code sample
import React from 'react';
import { CustomSvgIcon } from '../../util/CustomSvgIcon';
const Icon = ({ iconName }: IconProps) => {
const [menuIcon, setIcon] = React.useState<string | null>(null);
React.useEffect(() => {
if (iconName) {
import(`../../../assets/svg/${iconName}.svg`)
.then((bla) => setIcon(bla.default))
.catch((error) => console.log(error));
}
}, []);
return (
<CustomSvgIcon>
{menuIcon ? menuIcon : null}
</CustomSvgIcon>
);
};
This code works perfectly in the storybook, but when I use it in the react ponent it won't load images and that error pops. What am I doing wrong?
Share Improve this question asked Apr 28, 2020 at 19:41 MilosMilos 6193 gold badges10 silver badges24 bronze badges1 Answer
Reset to default 2This issue is solved with setting some rules in webpack.config. It was issue with .svg
. This is the rule that solved the issue:
{
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
options: {
icon: true,
},
},
],
}
本文标签: javascriptUncaught (in promise) TypeError Failed to fetch dynamically imported moduleStack Overflow
版权声明:本文标题:javascript - Uncaught (in promise) TypeError: Failed to fetch dynamically imported module - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744839545a2627837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论