Logo

dev-resources.site

for different kinds of informations.

How to Dynamically Apply Colors to SVGs in React Native

Published at
1/9/2025
Categories
reactnative
performance
themes
javascript
Author
ajmal_hasan
Author
11 person written this
ajmal_hasan
open
How to Dynamically Apply Colors to SVGs in React Native

SVGs are a powerful way to display scalable, vector-based graphics in React Native applications. However, customizing attributes like fill can sometimes be tricky if the SVG code isn't properly set up. A common issue arises when the fill property is hardcoded in the SVG, preventing dynamic color changes.

This blog explains why this happens and provides solutions to make SVG colors customizable in React Native.


Installation and setup:

a) Install:

yarn add react-native-svg && yarn add react-native-svg-transformer --dev
Enter fullscreen mode Exit fullscreen mode

b) In root directory create/update metro.config.js with:

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  transformer: {
    babelTransformerPath: require.resolve('react-native-svg-transformer'),
  },
  resolver: {
    assetExts: assetExts.filter((ext) => ext !== 'svg'),
    sourceExts: [...sourceExts, 'svg'],
  },
};

module.exports = mergeConfig(defaultConfig, config);
Enter fullscreen mode Exit fullscreen mode

Now you can use svg files in react native.


Understanding the Problem

Consider this example of an SVG file:

<svg
  width="4"
  height="18"
  viewBox="0 0 4 18"
  fill="none"
  xmlns="http://www.w3.org/2000/svg"
>
  <path
    d="M1.9987 4.41667C0.986176 4.41667 0.165365 3.59586 0.165365 2.58333C0.165365 1.57081 0.986176 0.75 1.9987 0.75C3.01122 0.75 3.83203 1.57081 3.83203 2.58333C3.83203 3.59586 3.01122 4.41667 1.9987 4.41667Z"
    fill="#EFF2F6"
  />
</svg>
Enter fullscreen mode Exit fullscreen mode

The fill="#EFF2F6" in the <path> element is hardcoded. If you pass a color prop to the SVG component, it wonโ€™t override this value. This makes the graphic static and unresponsive to dynamic styles.


Solution 1: Use currentColor

The simplest way to make the color property dynamic is to replace the hardcoded color value with currentColor. The currentColor value inherits the color property passed to the component.

Updated SVG:

<svg
  width="4"
  height="18"
  viewBox="0 0 4 18"
  fill="none"
  xmlns="http://www.w3.org/2000/svg"
>
  <path
    d="M1.9987 4.41667C0.986176 4.41667 0.165365 3.59586 0.165365 2.58333C0.165365 1.57081 0.986176 0.75 1.9987 0.75C3.01122 0.75 3.83203 1.57081 3.83203 2.58333C3.83203 3.59586 3.01122 4.41667 1.9987 4.41667Z"
    fill="currentColor"
  />
</svg>
Enter fullscreen mode Exit fullscreen mode

Usage in React Native:

<ThreeDots height="100%" color="#707070" />
Enter fullscreen mode Exit fullscreen mode

Now, the color property dynamically controls the color of the SVG.


Solution 2: Use React Native's react-native-svg

If you are using react-native-svg to handle SVGs, you can create a React component for the SVG (svg to react native tool) and pass the color as a prop.

Hereโ€™s an example:

import Svg, { Path } from 'react-native-svg';

const ThreeDots = ({ height = "100%", color = "#707070" }) => (
  <Svg width="4" height="18" viewBox="0 0 4 18" fill="none" xmlns="http://www.w3.org/2000/svg">
    <Path
      d="M1.9987 4.41667C0.986176 4.41667 0.165365 3.59586 0.165365 2.58333C0.165365 1.57081 0.986176 0.75 1.9987 0.75C3.01122 0.75 3.83203 1.57081 3.83203 2.58333C3.83203 3.59586 3.01122 4.41667 1.9987 4.41667Z"
      fill={color}
    />
  </Svg>
);

export default ThreeDots;
Enter fullscreen mode Exit fullscreen mode

Usage:

<ThreeDots height="100%" color="#FF0000" />
Enter fullscreen mode Exit fullscreen mode

The color prop is now passed dynamically to the <Path> element.


Conclusion

By using currentColor, react-native-svg, or customizing SVG components, you can dynamically control the fill property in your React Native app. This allows for greater flexibility and ensures your designs are dynamic and responsive to user interaction or theme changes.

themes Article's
30 articles in total
Favicon
How to Choose the Right Shopify Theme for Your Business Needs
Favicon
How to Resolve the Theme File Editor Missing Error in WordPress
Favicon
Here's an easy way to create a Magento 2 eCommerce custom theme
Favicon
Top WordPress Themes for 2024
Favicon
Migrating the VSCode theme generator to oklch
Favicon
LiveCodes Gets a Fresh Look, and Goes Multilingual!
Favicon
Tauri (4) - Get the theme switching function fixed
Favicon
10 Best Education WordPress Themes of 2024:
Favicon
How to Dynamically Apply Colors to SVGs in React Native
Favicon
Introducing the Goliat Theme: A project for the Community!
Favicon
Ensuring Consistency in Design Elements and Layouts
Favicon
Switching Modes: Implementing a Light-Dark Theme by CSS Only ๐ŸŒ—
Favicon
Cyberdev Obsidian Theme
Favicon
How to design custom product labels and badges | Shopify
Favicon
Theming with Styled-Components
Favicon
Welcome to ViraXpress (Open source Magento frontend)
Favicon
Black Fridayโ€™s Deals of the Year for Developers & Designers
Favicon
๐ŸŽจ All the Colors You Need with Tailwind CSS
Favicon
The Net Worth of Celebrities: A Glimpse into Wealth
Favicon
Why You Should Hire Shopify Theme Customization Experts
Favicon
What is Shopify App Development and Why Does Your E-commerce Business Need It Now?
Favicon
VSCode Theme Community
Favicon
How to make a custom theme for your vscode
Favicon
Sleek and Stylish Minimal WordPress Themes
Favicon
View transition theme animations
Favicon
Betckey Labels - Designed for Efficiency
Favicon
๐ŸŽจ VSCode Theme Generator: Create stunning themes using sacred geometry patterns
Favicon
Introducing My New VS Code Theme: Purple Puke ๐Ÿ’œ
Favicon
Best Tailwind CSS Template Builders
Favicon
Create Your Own Visual Vibe: Build Your VS Code Theme Extension with dharam-gfx! ๐ŸŽจ

Featured ones: