M
M
mimocrocodile2020-09-09 18:23:32
HTML
mimocrocodile, 2020-09-09 18:23:32

Why is the icon from the svg sprite not being inserted in a React project?

Having a problem using svg sprites in react app

I'm trying to insert an icon from a sprite, but it fails (it doesn't show up). I insert through svg use.
There are no problems in pure html - there the icon from the sprite is inserted and displayed.
The problem occurs only in a project written in react.

Inline svg are inserted and displayed without problems, the problem is only with the sprite. What could be the problem and what is it connected with?
5f58f36ce4ce2543562034.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
moody_good, 2020-09-09
@moody_good

Because react doesn't have HTML/XML, but JSX. The use tag is not supported. You can do this:

const useTag = '<use xlink:href="/icon/icons-sprite.svg#burgers" />';
return (
    <svg dangerouslySetInnerHTML={{__html: useTag }} />
);

dangerouslySetInnerHTML renders as HTML, not JSX. But it's better to use ready-made libs for svg. For example here:
https://www.npmjs.com/package/react-svg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question