当前第2页 返回上一页
src/components 文件夹下建一个 SvgIcon 文件夹 添加 index.jsx 文件
目录
index.jsx 组件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import React from "react" ;
import PropTypes from "prop-types" ;
import styles from "./style.less" ;
const SvgIcon = props => {
const { iconClass, fill } = props;
return (
<i aria-hidden= "true" className= "anticon" >
<svg className={styles[ "svg-class" ]}>
< use xlinkHref={ "#icon-" + iconClass} fill={fill} />
</svg>
</i>
);
};
SvgIcon.propTypes = {
iconClass: PropTypes.string.isRequired,
fill: PropTypes.string
};
SvgIcon.defaultProps = {
fill: "currentColor"
};
export default SvgIcon;
|
style.less
1 2 3 4 5 6 7 8 | .svg- class {
display: inline-block;
overflow: hidden;
font-size: 14px;
min-width: 14px;
width: 1em;
height: 1em;
}
|
5、使用
把要使用的的svg文件放到src/icons/svg目录下,使用的时候把svg名称给iconClass即可
1 2 3 4 5 6 7 | import React from "react" ;
import SvgIcon from "@/components/SvgIcon" ;
const Demo = () => {
const svgName= "content"
return <SvgIcon iconClass={svgName} />;
};
export default Demo;
|
相关学习推荐:javascript视频教程
以上就是react中如何优雅的使用svg的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
React redux是什么?
使用React怎么遍历数组
安装React脚手架会报错怎么办
React组件拆分的重要性
React中什么必须大写
为什么React需要绑定this
React中样式冲突怎么解决
React中get与post的区别是什么
webstorm不识别React怎么办
React怎么添加css样式
更多相关阅读请进入《React》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » react中如何优雅的使用svg