svg
SVGComponent
Warning: SVGComponent: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead
next.config.mjs
检查填充属性(svg,path等)并将所有值替换为 currentColor 以扩展当前文本颜色
// pnpm add @svgr/webpack
// next.config.mjs
webpack: (config) => {
config.module.rules.push({
test: /\.svg$/i,
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false, // important
},
},
},
],
},
},
});
return config;
},
组件中动态导入 svg 图标
/svg/facebook.svg
interface IProps {
name?: 'facebook ' | 'google';
style?: React.CSSProperties;
className?: string;
width?: number;
height?: number;
}
export const Icon = async ({ name, ...rest }: IProps) => {
const Icon = await import(`./svg/${name}.svg`).then((icon) => icon.default);
return <Icon {...rest} />;
};