react-native-svg-src
Render an SVG Image from local file in the src directory. This lib is fork from react-native-svg-uri@1.2.3. But I will keep updating for this one.
Install library from npm
npm install react-native-svg-src --save
Props
Prop | Type | Default | Note |
---|---|---|---|
src |
String |
undefined | the content of SVG |
fill |
String |
undefined | override the color by custom fill |
TODO
- write document in Chinese
- add Symbol tag support
- rotation(rotate in transform) seems to be unsupport, I will have a check, and reduce unnecessary rotation
Usage
Maybe you feel why the src is the content of svg instead of the path of static file
That's because this lib was fork on react-native-svg-uri@1.2.3. extends his everything includes his known bug with prop source:
[ANDROID] There is a problem with static SVG file on Android, Works OK in debug mode but fails to load the file in release mode. At the moment the only workaround is to pass the svg content in the svgXmlData prop.
Don't worry. Here is the temp solution. I promise I will proffer easiest usage when I'm free.
I suppose you put all svg in one folder. Make a getSvg.js as fileReader, launch it once before you developing.
│
│─ getSvg.js
│
└─ SVGFOLDER ─
│─ foo.svg
│─ bar.svg
Here is the getSvg.js from jianshu.com
// getSvg.jsvar fs = ;var path = ;const svgDir = path; { return { fs; };} { return { fs; };} ;
After run it, your files tree should looks like below
│
│─ svgs.js
│─ getSvg.js
│
└─ SVGFOLDER ─
│─ foo.svg
│─ bar.svg
The svgs.js export an object<K,V>. The K means the svg file name, the V means the content of the svg.
OK. Let's see the example
// Svg.js;;; { const src = thisprops; let svgXmlData = svgsthispropssrc; if !svgXmlData let err_msg = `no such icon which called ""`; throw err_msg; return <SvgSrc src=svgXmlData /> }
// example.js;;; { return <View style= height: 100 width: 100> /* foo means foo.svg under your SVGFOLDER */ <Svg src="foo" /> </View> }