react-native-svg-transformer vs react-native-svg-charts vs react-native-svg
React Native SVG Libraries
react-native-svg-transformerreact-native-svg-chartsreact-native-svgSimilar Packages:

React Native SVG Libraries

These libraries enhance the capabilities of React Native applications by providing tools for rendering SVG graphics, creating charts, and transforming SVG files into React components. They enable developers to create visually rich and interactive applications by leveraging the power of SVG, which is scalable and resolution-independent. This is particularly useful for mobile applications where performance and visual fidelity are crucial. Each library serves a unique purpose, from basic SVG rendering to advanced charting functionalities, making them essential for developers looking to implement complex visualizations in their apps.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-native-svg-transformer908,3801,73115.3 kB89a month agoMIT
react-native-svg-charts76,4732,399-2196 years agoMIT
react-native-svg07,9603.75 MB19723 days agoMIT

Feature Comparison: react-native-svg-transformer vs react-native-svg-charts vs react-native-svg

SVG Rendering

  • react-native-svg-transformer:

    react-native-svg-transformer focuses on the convenience of importing SVG files as React components. It transforms SVG files at build time, allowing developers to use them seamlessly in their components without manual conversion.

  • react-native-svg-charts:

    react-native-svg-charts leverages react-native-svg for rendering charts. It simplifies the process of creating various chart types while ensuring that the underlying SVG rendering is handled efficiently, allowing for smooth animations and transitions in data visualization.

  • react-native-svg:

    react-native-svg provides a comprehensive set of SVG elements, including paths, circles, rectangles, and more. It allows for detailed customization of SVG properties, enabling developers to create complex and interactive graphics that are essential for modern mobile applications.

Charting Capabilities

  • react-native-svg-transformer:

    react-native-svg-transformer does not offer charting capabilities but facilitates the use of SVG assets in conjunction with other libraries, including react-native-svg-charts, by allowing SVG files to be imported as components.

  • react-native-svg-charts:

    react-native-svg-charts excels in providing a variety of pre-built chart types and customization options. It simplifies the process of data visualization, making it easy to create responsive and interactive charts with minimal code.

  • react-native-svg:

    react-native-svg does not provide built-in charting capabilities but serves as the foundation for libraries like react-native-svg-charts that do. It allows developers to create custom charts using SVG elements if they prefer a more hands-on approach.

Ease of Use

  • react-native-svg-transformer:

    react-native-svg-transformer is straightforward to use, allowing developers to import SVG files directly into their components. This reduces the complexity of managing SVG assets and enhances productivity.

  • react-native-svg-charts:

    react-native-svg-charts is designed for ease of use, providing a high-level API that abstracts much of the complexity involved in creating charts. Developers can quickly implement charts with minimal setup, making it accessible for those new to data visualization.

  • react-native-svg:

    react-native-svg has a moderate learning curve, especially for developers familiar with SVG concepts. It requires understanding of SVG syntax and properties, but once mastered, it offers powerful capabilities for rendering graphics.

Performance

  • react-native-svg-transformer:

    react-native-svg-transformer focuses on build-time transformations, which can slightly impact the build process but does not affect runtime performance. It allows for efficient use of SVG assets without runtime overhead.

  • react-native-svg-charts:

    react-native-svg-charts is built on top of react-native-svg and inherits its performance optimizations. It is designed to handle large datasets efficiently, ensuring that charts render smoothly even with dynamic updates.

  • react-native-svg:

    react-native-svg is optimized for performance in mobile environments, ensuring that SVG rendering is efficient and does not hinder application responsiveness. It supports hardware acceleration where possible, which is crucial for smooth animations.

Community and Support

  • react-native-svg-transformer:

    react-native-svg-transformer has a smaller community but is well-documented. It integrates seamlessly with other libraries, making it easy to find support within the broader React Native ecosystem.

  • react-native-svg-charts:

    react-native-svg-charts benefits from the popularity of react-native-svg and has a supportive community. However, it may have fewer resources compared to the core library, so developers might need to rely on the main library's documentation for advanced charting techniques.

  • react-native-svg:

    react-native-svg has a large and active community, providing extensive documentation, examples, and support. This makes it easier for developers to find resources and solutions to common issues.

How to Choose: react-native-svg-transformer vs react-native-svg-charts vs react-native-svg

  • react-native-svg-transformer:

    Choose react-native-svg-transformer if you want to import SVG files directly as React components in your project. This library simplifies the process of using SVG assets by allowing you to import them as components, which can be styled and manipulated just like any other React component.

  • react-native-svg-charts:

    Choose react-native-svg-charts if your primary goal is to create various types of charts (like line, bar, pie, etc.) with minimal effort. This library builds on react-native-svg and provides a high-level API for easily creating and customizing charts, making it ideal for data visualization needs.

  • react-native-svg:

    Choose react-native-svg if you need a robust library for rendering SVG images and shapes directly in your React Native applications. It provides a comprehensive set of SVG elements and is the foundation for other libraries that depend on SVG rendering.

README for react-native-svg-transformer

react-native-svg-transformer NPM version Downloads per month contributions welcome

React Native SVG transformer allows you to import SVG files in your React Native project the same way that you would in a Web application when using a library like SVGR to transform your imported SVG images into React components.

This makes it possible to use the same code for React Native and Web.

Usage

Import your .svg file inside a React component:

import Logo from "./logo.svg";

You can then use your image as a component:

<Logo width={120} height={40} />

Demo / Expo demo (iOS/Android/Web)

Installation and configuration

Step 1: Install react-native-svg library

Make sure that you have installed the react-native-svg library:

Step 2: Install react-native-svg-transformer library

npm install --save-dev react-native-svg-transformer

or

yarn add --dev react-native-svg-transformer

Step 3: Configure the react native packager

For Expo SDK v41.0.0 or newer

Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

metro.config.js:

const { getDefaultConfig } = require("expo/metro-config");

module.exports = (() => {
  const config = getDefaultConfig(__dirname);

  const { transformer, resolver } = config;

  config.transformer = {
    ...transformer,
    babelTransformerPath: require.resolve("react-native-svg-transformer/expo")
  };
  config.resolver = {
    ...resolver,
    assetExts: resolver.assetExts.filter((ext) => ext !== "svg"),
    sourceExts: [...resolver.sourceExts, "svg"]
  };

  return config;
})();

For React Native v0.72.1 or newer

Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

metro.config.js:

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

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

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

module.exports = mergeConfig(defaultConfig, config);

For React Native v0.59 or newer

Merge the contents from your project's metro.config.js file with this config (create the file if it does not exist already).

metro.config.js:

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts }
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve(
        "react-native-svg-transformer/react-native"
      )
    },
    resolver: {
      assetExts: assetExts.filter((ext) => ext !== "svg"),
      sourceExts: [...sourceExts, "svg"]
    }
  };
})();

React Native projects using Expo modules

Some React Native projects are using Expo modules without using expo-cli.

In such projects Expo's transformer is selected by default, and can be overwritten to correctly use React Native's transformer by adding react-native to the require path:

-require.resolve("react-native-svg-transformer")
+require.resolve("react-native-svg-transformer/react-native")

You can also force Expo's transformer to always be used:

-require.resolve("react-native-svg-transformer")
+require.resolve("react-native-svg-transformer/expo")

Using TypeScript

If you are using TypeScript, you need to add this to your declarations.d.ts file (create one if you don't have one already):

declare module "*.svg" {
  import React from "react";
  import { SvgProps } from "react-native-svg";
  const content: React.FC<SvgProps>;
  export default content;
}

Configuring SVGR (how SVG images get transformed)

SVGR has a configuration file, which makes it possible for you to customize how SVG images get transformed to React/React Native.

Read more about the configuration options: Configuring SVGR and SVGR options.

For example, if you want to change SVG image's fill color from red to currentColor (keep in mind that this will be used for all SVG images in your app).

.svgrrc (create the file in your project's root folder if it does not exist)

{
  "replaceAttrValues": {
    "red": "currentColor"
  }
}

Changing SVG fill color in JS Code

Edit your .svgrrc file and include a line for replaceAttrValues that matching a hex code to {props.fill}

{
  "replaceAttrValues": {
    "#000": "{props.fill}"
  }
}

And then make sure your path tag inside the SVG file fill attribute is the hex code (in this case #000).

<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M2.965 6.0925C4.045 8.215 ..." fill="#000"/>
</svg>

You can then use your image as a component:

<Logo width={120} height={40} fill={"any color"} />

Usage with Jest

To use Jest to test your React Native components that import .svg images, you need to add this configuration that mocks the SVG images that are transformed to React components:

__mocks__/svgMock.js:

module.exports = "SvgMock";

Then, depending on where you have your Jest configuration:

package.json:

{
  "jest": {
    "moduleNameMapper": {
      "\\.svg": "<rootDir>/__mocks__/svgMock.js"
    }
  }
}

or

jest.config.js:

module.exports = {
  moduleNameMapper: {
    "\\.svg": "<rootDir>/__mocks__/svgMock.js"
  }
};

Rendering custom fonts in iOS

At the moment react-native-svg does not support custom font families in iOS right out of the box. A workaround is to take your .svg with custom fonts and convert it to outlines. This will replace text tags for path tags in your .svg file.

Dependencies

In addition to React Native, this transformer depends on the following libraries: