React Native SVG Transformer enables developers to import SVG files directly into their React Native projects, treating them as React components. This mimics the development experience of web applications using libraries like SVGR, facilitating code sharing between web and mobile platforms. The package is currently at version 1.5.3 and demonstrates an active release cadence, frequently updating to support new versions of React Native and Expo SDK, as indicated by multiple releases within recent months addressing compatibility issues. It's a crucial tool for projects aiming for scalable vector graphics that can be dynamically styled and manipulated, differentiating itself by seamlessly integrating SVG assets into the Metro bundler workflow for both bare React Native and Expo projects.
npm install react-native-svg-transformerVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to install `react-native-svg-transformer` and `react-native-svg`, configure Metro for a bare React Native project, and then import and render an SVG file as a React component.
Upgrade `react-native` to `0.59.0` or newer and `react-native-svg` to `12.0.0` or newer before updating `react-native-svg-transformer`.
Carefully follow the installation instructions in the README for your specific project setup. Ensure you use `react-native-svg-transformer/react-native` for bare React Native and `react-native-svg-transformer/expo` for Expo, matching the relevant `getDefaultConfig` import style.
Utilize SVGR's configuration (via a `.svgrrc` file) to generate unique ID prefixes. The `@svgr/plugin-svgo` dependency can be configured for this. For example, add `"svgoConfig": { "plugins": [{ "name": "prefixIds" }] }` to your `.svgrrc` to ensure unique IDs.Create a `declarations.d.ts` file in your project root with content like: `declare module "*.svg" { import React from "react"; import { SvgProps } from "react-native-svg"; const content: React.FC<SvgProps>; export default content; }`Ensure `metro.config.js` is correctly configured: `assetExts` should filter out 'svg', and `sourceExts` should include 'svg'. Restart Metro with `npm start -- --reset-cache` or `yarn start --reset-cache`.
For iOS, navigate to your `ios` directory and run `pod install`. For Android, ensure a clean build by running `cd android && ./gradlew clean` and then rebuilding your app. Ensure `react-native-svg` is properly installed and linked according to its documentation.
Verify that `babelTransformerPath` is set correctly to either `require.resolve("react-native-svg-transformer/react-native")` or `require.resolve("react-native-svg-transformer/expo")` based on your project type and that `react-native-svg-transformer` is installed. Check for conflicts if multiple transformers are used.For Expo projects, ensure your `metro.config.js` uses `require.resolve("react-native-svg-transformer/expo")`. If the issue persists, review Expo SDK and `react-native-svg-transformer` compatibility.