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 以扩展当前文本颜色

  1. // pnpm add @svgr/webpack
  2. // next.config.mjs
  3. webpack: (config) => {
  4. config.module.rules.push({
  5. test: /\.svg$/i,
  6. loader: '@svgr/webpack',
  7. options: {
  8. svgoConfig: {
  9. plugins: [
  10. {
  11. name: 'preset-default',
  12. params: {
  13. overrides: {
  14. removeViewBox: false, // important
  15. },
  16. },
  17. },
  18. ],
  19. },
  20. },
  21. });
  22. return config;
  23. },

组件中动态导入 svg 图标

/svg/facebook.svg

  1. interface IProps {
  2. name?: 'facebook ' | 'google';
  3. style?: React.CSSProperties;
  4. className?: string;
  5. width?: number;
  6. height?: number;
  7. }
  8. export const Icon = async ({ name, ...rest }: IProps) => {
  9. const Icon = await import(`./svg/${name}.svg`).then((icon) => icon.default);
  10. return <Icon {...rest} />;
  11. };