React Native SVG
React Native SVG is a comprehensive library that brings Scalable Vector Graphics (SVG) support to React Native applications across various platforms, including iOS, Android, macOS, and Windows, with a compatibility layer for the web. The current stable version is 15.15.4. The project maintains an active release cadence, frequently publishing patch and minor versions to address bugs, improve performance, and ensure compatibility with the latest React Native nightly builds and stable releases. A key differentiator is its extensive support for most SVG elements and properties, facilitating the rendering of complex vector graphics directly within native mobile applications. It also simplifies the process of integrating existing SVG assets by supporting conversion tools like SVGR. Furthermore, `react-native-svg` has adapted to React Native's evolving architecture, including early and ongoing support for the Fabric rendering system, though specific versions are required for compatibility.
Common errors
-
Invariant Violation: requireNativeComponent: "RNSVG<ComponentName>" was not found in the UIManager.
cause The native module for `react-native-svg` was not correctly linked or built into your React Native application.fixFor iOS/macOS, navigate to the `ios` directory and run `pod install`, then rebuild your application. Ensure that you have followed the linking instructions for your specific React Native version and platform (e.g., `npx react-native-windows-init --overwrite` for Windows). -
ReferenceError: Buffer is not defined
cause An older version of `react-native-svg` attempted to use the Node.js `Buffer` API for base64 decoding in a React Native environment where it's not globally available.fixUpgrade `react-native-svg` to version `15.15.3` or newer, which uses `atob` for decoding, resolving this issue. -
Build errors on iOS/macOS (e.g., 'RCTImage' use-after-free crash, 'UIImage' build issues)
cause Platform-specific breaking changes in React Native or Xcode, or incorrect build configurations for `react-native-svg`.fixEnsure you are on the latest `react-native-svg` version (`15.15.3` or newer fixes recent iOS issues). Clean your build folder (`Xcode -> Product -> Clean Build Folder`), reset Metro cache, and reinstall pods (`cd ios && rm -rf Pods && pod install`). For macOS, specifically check for patches in patch versions like `15.15.2`.
Warnings
- breaking Version 15.13.0 and newer drops support for React Native versions older than 0.78. Ensure your React Native project is updated.
- breaking Support for Fabric (React Native's new rendering system) has specific version requirements. Using incompatible versions can lead to build failures or runtime errors.
- gotcha A performance regression was introduced in `v15.15.0` related to shadow node propagation and `ForeignObject` rendering.
- gotcha An iOS `RCTImage use-after-free` crash was present in some versions due to breaking changes in React Native.
- gotcha Older versions might encounter 'ReferenceError: Buffer is not defined' due to attempts to use Node.js `Buffer` for decoding, which is not available in React Native environments by default.
Install
-
npm install react-native-svg -
yarn add react-native-svg -
pnpm add react-native-svg
Imports
- Svg
import Svg from 'react-native-svg'
import { Svg } from 'react-native-svg' - Rect
import { Rect } from 'react-native-svg' - SvgXml
const { SvgXml } = require('react-native-svg')import { SvgXml } from 'react-native-svg'
Quickstart
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { Svg, Rect, Circle, Line, Text } from 'react-native-svg';
const MySVGComponent = () => {
return (
<View style={styles.container}>
<Svg height="200" width="200" viewBox="0 0 100 100">
{/* Simple Red Rectangle */}
<Rect
x="10"
y="10"
width="80"
height="30"
stroke="red"
strokeWidth="2"
fill="rgba(255,0,0,0.5)"
/>
{/* Blue Circle */}
<Circle cx="50" cy="70" r="25" fill="blue" stroke="none" />
{/* Text */}
<Text
x="50"
y="50"
textAnchor="middle"
fill="white"
fontSize="10"
>
Hello SVG!
</Text>
{/* Line */}
<Line
x1="0"
y1="0"
x2="100"
y2="100"
stroke="purple"
strokeWidth="1"
/>
</Svg>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
export default MySVGComponent;