gregberge / svgr

Transform SVGs into React components 🦁
https://react-svgr.com
MIT License
10.59k stars 422 forks source link

How to use svg sprites? #899

Open Karina1703 opened 1 year ago

Karina1703 commented 1 year ago

I would like something like that

import Sprite from "../../../public/sprite.svg";

const page = () => {
  return (
    <div>
      <Sprite />

      <svg>
        <use href={`#like`} />
      </svg>

    </div>
  );
};

But this doesn't work

gregberge commented 1 year ago

It is not supported.

zousandian commented 1 month ago

For anyone encountering the same issue, SVGO removes SVG defs by default. You can override the svgoConfig to make it work.

FYI: https://github.com/svg/svgo/issues/1896#issuecomment-1866798474

vite config example:

import svgr from '@svgr/rollup';

...
  plugins: [
     svgr({
        svgoConfig: {
          plugins: [
            {
              name: 'preset-default',
              params: {
                overrides: {
                  removeHiddenElems: false,
                  cleanupIds: false,
                },
              },
            },
          ],
        },
      }),
 ],
...