react-native-svg-transformer: fill prop not working for svg file!
Hey! @kristerkari I was looking for a way to change the color of the svg icon ( just like in #4 ). Going through the docs, so far I’ve tried this:
Imported the svg file in the project.
import Shop from '../Components/Assets/shop.svg';
Used the svg file.
<Shop width={'100%'} height={'30%'} fill={'red'} />
Created a .svgrrc (no extension) file in the root of the project containing:
{
"replaceAttrValues": {
"#748A9D": "{props.fill}"
}
}
My project dependencies: “react-native”: “0.63.2”, “react-native-svg”: “^12.1.0”, “react-native-svg-transformer”: “^0.14.3”, “semver”: “^7.3.2” “@svgr/core”: “^5.4.0”, “path-dirname”: “^1.0.2”,
Am I missing something?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 29 (2 by maintainers)
@KPS250 I exported my SVG from Figma and it set the property
fillinside the<svg>tonone.When that happen, every
<path>(which are inside<svg>) are not getting colored, even if they are<path fill="#000000">.To fix the issue, I had to change
fill="none"tofill="#000000"from the<svg>element.TLDR Ensure
<svg>elements havefill="#000000"and nothing elseAlso I fixed my problem by removing fill=“white” in the
<path></path>line and changing the<svg fill="none">to<svg fill="#000000">also I restarted the react-native server and actual appExample: Old
New
I had the same issue in Android, I’m not using expo. The resolution for me was
$ cd android,$ ./gradlew clean$ yarn start --reset-cache$ yarn androidAfter I’ve done this, I didn’t have to clean the build anymore. I just had to
reset-cachewhen I had to add more configs.try removing all ‘fill=…’ from paths only and leave fill=‘none’ in main svg tag
fill prop not working for svg file! || How to assign color to svg
Solution:
I solved this issue follow me to solve this
Steps
Open the svg in editor as you can see in picture below
Remove all fill prop from paths only (Only from path tags) , And ensure that svg tag’s prop fill =‘null’ as you can see in picture below
Save that changes in svg file
Import that svg where you want to use
Pass fill={
Color of your Choice} prop to that svg e.g<NoStoreFoundSvg fill={'red'} />Save all files and you will get your desired result Happy Coding!
Also having the same problem, this config:
… does not work with this
I’m having to replace “#02024D” with “@fill” in svg file and then
This is how i achieved.
no need of .svgrrc file in your svg file remove all fills then your svg will be invisible, then you can use the fill prop and change the colors,
i’m using react-native-svg-transformer without using react-native-svg which i found very heavy in term of size, so i can resize and change the stroke color also the fill color, but just instead of passing a fill prop, just pass color as seen below, it works perfectly
the svg code
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><title>profile</title><g fill="currentColor" class="nc-icon-wrapper"><path d="M38,37H26A19.021,19.021,0,0,0,7,56a1,1,0,0,0,.594.914C7.97,57.081,16.961,61,32,61s24.03-3.919,24.406-4.086A1,1,0,0,0,57,56,19.021,19.021,0,0,0,38,37Z"></path><path data-color="color-2" d="M32,32c8.013,0,14-8.412,14-15.933a14,14,0,1,0-28,0C18,23.588,23.987,32,32,32Z"></path></g></svg>dependencies
metro.config.js file to add in the root
In Expo, it works after I clear the cache.
expo r -cIt does not seem like the hot reload pick up changes in .svgrrc, so I have to clear the cache every time I make a change with the command above.
Just removed the fill prop inside path and give path=“none” in the parent svg tag like this.
If you want to give the default color to svg or want to fill prop working as well you can give the default color in the fill prop
I can confirm that the color replacements do not work if you have fill=“none” on the svg tag. To solve it, you can either remove the attribute manually or enable svgo in your .svgrrc file
The default SVGO config includes the removeUselessStrokeAndFill plugin which will remove the fill=“none” from the svg tag. The color replacements then work.
in SVG children add fill=“currentColor”, then you can style
this working for me , thank you.
@Wintus thank you very much this worked for me
Sure, sorry for giving with lack of info. I had the exact similar problem with @cmcodes1
Here is my svgrrc:
Here is my SVG icon:
Here is how i use the icon:
Version of Libraries: “react-native”: “0.62.2”, “react-native-svg”: “^12.1.0”, “react-native-svg-transformer”: “^0.14.3”,