Adding SVGs in React Native Expo

devninjaade

Adebayo Temitope

Posted on February 19, 2021

Adding SVGs in React Native Expo

As a developer you would have definitely worked with an SVG (Scalable Vector Graphics) in any project you're working on but in React Native (Expo) SVGs needs extra steps to work on an app.

1. Create Your App

expo init test-svg
Enter fullscreen mode Exit fullscreen mode

2. Move into the app folder

cd test-svg
Enter fullscreen mode Exit fullscreen mode

3. Add 'react-native-svg'

npm i react-native-svg
Enter fullscreen mode Exit fullscreen mode

4. Create a JS file 'TestSvg.js'

Paste the following Code below

import * as React from "react";
import { SvgXml } from "react-native-svg";

export default function TestSvg(){  
  const svgcode = `paste your <svg> code here`;
  const Svg = () => <SvgXml xml={svgcode} width="set the width here" 
  height="set the height here" />;  

  return <Svg />;
}
Enter fullscreen mode Exit fullscreen mode

5. Import into your 'App.js' file

You can add to any other file

import * as React from "react";
import { View } from "react-native";
import TestSvg from "./TestSvg";

export default function App() {
return (
  <View>
    <TestSvg />
  </View>
 )
}
Enter fullscreen mode Exit fullscreen mode

That's all, Your SVG file would work.

💖 💪 🙅 🚩
devninjaade
Adebayo Temitope

Posted on February 19, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Querying data using React Query
javascript Querying data using React Query

October 2, 2022

Simple alert in React Native
javascript Simple alert in React Native

March 11, 2022

Adding SVGs in React Native Expo
javascript Adding SVGs in React Native Expo

February 19, 2021

Styling the React Native Way
javascript Styling the React Native Way

January 14, 2019