【发布时间】:2021-07-18 20:26:09
【问题描述】:
我正在尝试将 svg 作为 React 组件导入。我已经安装了svgr。我还添加了 svg 类型定义:
declare module '*.svg' {
import React = require('react');
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
const src: string;
export default src;
}
以上内容也包含在我的tsconfig.json
当我尝试导入 svg 时:
import { ReactComponent as Logo } from "logo.svg";
// inside render
return <Logo/>
它抛出:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports
非常感谢任何帮助!
【问题讨论】:
-
为什么要导出像
component这样的SVG文件?以及你如何使用Logo? -
@Viet:否则打字稿不知道如何导入
<Logo/>
标签: reactjs typescript svg webpack