Skip to content
Advertisement

Whats the best way to add an SVG to ReactJS

I want to create a component that displays an SVG in React. I am not having any luck, does anyone know a good practice for turns SVG’s into React components.

I tried adding them to <img /> tags and <svg /> tags but i keep getting a broken image icon?

Advertisement

Answer

the best practice that I have found is that save svg in simple SVG tag and import it like this in your project

import { ReactComponent as EmptyWallet } from 'src/assets/svgs/EmptyWallet.svg';

EmptyWallet.svg file will look like this

<svg>
<path d="M29.73,17.66H0V47.79H29.73V59.28c.21,5,3.88,6.78,8.42,5.25a2.47,2.47,0,0,0,.7-.37c11.47-9,19.37-18,30.84-27l.16-.13c3.49-3.18,2.46-6.8-.93-9.47L40.81,2.61A13.25,13.25,0,0,0,36.65.28C34-.51,31.54.37,30.41,3a10,10,0,0,0-.68,3.93c0,3.57-.05,7.16,0,10.73ZM93.15,64h29.73V94.16H93.15v11.48c-.21,5-3.88,6.79-8.42,5.25a2.24,2.24,0,0,1-.7-.37c-11.47-9-19.37-18-30.84-27L53,83.38c-3.49-3.17-2.46-6.8.93-9.46L82.07,49a13,13,0,0,1,4.16-2.32c2.66-.8,5.11.08,6.24,2.71a10,10,0,0,1,.68,3.94c0,3.56.06,7.16,0,10.72Z" />
</svg>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement