Skip to content

Instantly share code, notes, and snippets.

@jokemmy
Last active August 10, 2017 03:40
Show Gist options
  • Save jokemmy/3c2241d8c9f4fc88a14082511febd976 to your computer and use it in GitHub Desktop.
Save jokemmy/3c2241d8c9f4fc88a14082511febd976 to your computer and use it in GitHub Desktop.
Use SVG icon with svg-sprite-loader
// 现用方案
function IconSVG({ xlinkHref, className, ...props }) {
const clsName = className ? `${styles.iconSVG} ${className}` : styles.iconSVG;
return (
<svg aria-hidden="true" className={clsName} {...props}>
{/**
* 会出现 bug 的场景:使用 browserHistory,并且页面内使用 href 属性非空的 <base /> 标签
* 如果不加 pathname,在火狐或者某国产双核浏览器中以及安卓版微信浏览器中图标不显示
*/}
<use xlinkHref={location.pathname + xlinkHref} />
</svg>
);
}
// svg-sprite-loader 官方提供的方法
function IconSVG({ xlinkHref, className, ...props }) {
const clsName = className ? `${styles.iconSVG} ${className}` : styles.iconSVG;
return (
<svg aria-hidden="true" className={clsName} {...props}>
<use xlinkHref={xlinkHref} />
</svg>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment