React Native SVG
Using SVG (Scalable Vector Graphics) in React Native requires a bit of additional setup because React Native doesn’t support SVG out of the box. You can use SVGs in your React Native app by using a library like react-native-svg
. This library provides SVG support for React Native and allows you to use SVG files or SVG code directly in your components.
Installation
Npm:
1 | npm install react-native-svg |
Yarn:
1 | yarn add react-native-svg |
Linking (for React Native versions < 0.60):
1 | react-native link react-native-svg |
For React Native 0.60 and above, auto linking should take care of this.
Using SVGs in Your App
There are two main ways to use SVGs with react-native-svg
:
A. Using Inline SVG Code
Just write the SVG code as other components does. Here’s an example:
1 | import React from 'react'; |
In this example, Svg
, Circle
, and Rect
are components provided by react-native-svg
that correspond to their SVG equivalents.
B. Using SVG Files
If you have SVG files and want to use them directly in your components, you need a loader called react-native-svg-transformer
to import them as React components.
Step 1: Install library
using npm:
1 | npm install --save-dev react-native-svg-transformer |
or yarn:
1 | yarn add --dev react-native-svg-transformer |
Step 2: Configure the react native packager
You need to configure the Metro bundler to use the transformer. Modify your metro.config.js
to look like this:
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).
1 | const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config"); |
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).
1 | const { getDefaultConfig } = require("expo/metro-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).
1 | const { getDefaultConfig } = require("metro-config"); |
Using TypeScript
If you are using TypeScript, you need to add this to your declarations.d.ts
file at your root dir (create one if you don’t have one already), so you will get the type hint:
1 | declare module "*.svg" { |
More custom options
Check the README in their repo
Usage
Here’s a example to use it:
1 | import React from 'react'; |