Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - how to use local SVG file (and color it)

I have some .svg icons locally that I'd like to use in my RN project.

An example is an .svg icon like this (opened in Chrome): enter image description here

I'd like to display this file (as well as having control in specifying fill color for it) -- is there any way to do this in React Native?

(i've tried 'react-native-remote-svg' component but that doesn't give me any control over coloring)

Thanks!

like image 682
SpicyClubSauce Avatar asked Apr 04 '18 21:04

SpicyClubSauce


People also ask

How do I use local SVG in react-native?

Open up the project in your favorite editor and start by importing the Svg and Circle components from react-native-svg, as shown below. import Svg, { Circle } from 'react-native-svg'; The <Svg> component is a parent component that is needed to render any SVG shape.

Can svgs be colored?

You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser. If you want to change your SVG image, you have to load it using <object> , <iframe> or using <svg> inline.

Can we use SVG in react-native?

Scalable Vector Graphic (SVG) is an image format that uses vector graphics to display the image you see. They are now popular among developers because they scale to any resolution.


1 Answers

You can use react-native-svg-transformer to import svgs in to your code. Once you've set that up, you'll be able to do:

import ProfileIcon from './images/profile.svg'

You can then use ProfileIcon as a component.

To control the colour, edit the svg file and change the fill or stroke attributes to "currentColor" (so <polygon fill="currentColor" ...>). You can then set the color style on your svg element and it'll use that colour: <ProfileIcon style={{ color: '#f80' }}/>

I'm using react-native-svg 9.3.5.

like image 140
hrunk Avatar answered Sep 20 '22 19:09

hrunk