svg - 如何将 svg 导入 antd -> Icon 组件

标签 svg create-react-app antd

嘿,我正在尝试将我自己的 svg 导入 antd -> Icon 组件
就像在 documentation但我有一个异常(exception)

InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/archive-solid.3c305173.svg') is not a valid name.



我使用的是 create react app 2.1.1 和 antd 版本 3.10.3
我不想创建 react 应用程序弹出,我不访问 webpack
有任何想法吗。
代码:
import React, { Component } from "react";
import { Layout, Menu, Icon } from "antd";
import ArchiveSVG from "../../../img/icons/archive-solid.svg";

const { Sider } = Layout;

class SideBar extends Component {
state = {
  collapsed: false
};

onCollapse = collapsed => {
  this.setState({ collapsed });
};

render() {
 return (
   <Sider
     collapsible
     collapsed={this.state.collapsed}
     onCollapse={this.onCollapse}
   >
     <div className={styles.logo} />
     <Menu theme="dark" defaultSelectedKeys={["1"]} mode="inline">
       <Menu.Item key="4">
         <Icon component={ArchiveSVG} />
         <span>Products</span>
       </Menu.Item>
     </Menu>
   </Sider>
 );
}
}

最佳答案

import Icon from '@ant-design/icons';
import { ReactComponent as USAFlagIcon } from "./usa-flag.svg";

const USFlag = () => <Icon component={USAFlagIcon} />
同样要更改自定义图标的颜色,您需要删除 SVG 属性。 (我正在使用 svgo .. * how to us it? * config file )

关于svg - 如何将 svg 导入 antd -> Icon 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322676/

相关文章:

reactjs - React 应用程序无法在 IE 11 浏览器中运行

javascript - react DVA JS 框架 : Reset entire state application

reactjs - Electron 中的 Antd Datepicker - 无法读取 null 的属性 'year'

node.js - 如何在不使用docker安装Node js的情况下开发Node JS(create-react-app)

ruby-on-rails - 如何修复默认 "rails new app"和 "npx create-react-app"的这些依赖性问题?

javascript - 如何从嵌入式 svg 调用父 html 文档中的函数

javascript - d3js 图形代码以不同的顺序显示不同数据的 y 轴

reactjs - defaultValue 或值不适用于 FormItem ANTD

javascript - 如何创建像指标一样的循环进度(饼图)

javascript - 我的 d3 折线图中的画笔功能未按预期工作