Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert svg to react-native-svg

What is the easiest way to do this? I found plenty of svg to JSX converters, which is what I want, but that doesn't work in react-native. I need to convert the svg code to something I can display in my app using react-native-svg. Thanks!

like image 729
BlackKnight Avatar asked Oct 10 '18 15:10

BlackKnight


People also ask

How do I import SVG into react-native?

Open up the project in your favorite editor and start by importing the Svg and Circle components from react-native-svg, as shown below. import Svg, { Circle } from 'react-native-svg'; The <Svg> component is a parent component that is needed to render any SVG shape.

How create SVG component in react-native?

Install react-native-svg-transformer by running this command yarn add react-native-svg-transformer --dev or using npm if you prefer npm i react-native-svg react-native-svg-transformer --save-dev. Install babel-plugin-inline-import by running yarn add babel-plugin-inline-import --dev. You need to update your metro.

Is SVG better than PNG in react-native?

Show activity on this post. Prefer PNG over SVG for react-native apps because its rendering is less CPU intensive, and comparing to web apps user don't need to load all images on each app launch but only on installation, so the size doesn't matter that much.


1 Answers

I can think of the following options. The one you use will depend on the amount of files you have to convert.

Option 1 (few files)

Copy-paste your svg code on this site and check the React Native checkbox. This will give you the code which you can then use with react-native-svg

Use that output within the following code (replace the SvgComponent with what was generated):

import React, { Component } from 'react';
import { View } from 'react-native';
import Svg, { Circle, Path } from 'react-native-svg';

const SvgComponent = props => (
  <Svg viewBox="0 0 48 1" style={props.style}>
    <Path d="M0 0h48v1H0z" fill="#063855" fillRule="evenodd" />
  </Svg>
);

class MySVGIcon extends Component {
  render() {
    const { style } = this.props;
    const component = SvgComponent(style);

    return <View style={style}>{component}</View>;
  }
}

export default MySVGIcon;

Option 2 (many files)

Instead of converting them, you can embed them directly in your code with react-native-remote-svg.

For instance you can do this:

import Image from 'react-native-remote-svg';

class MyImageClass extends Component {

  render () {
    // Embed code or read a SVG file...
    const mySVGImage = '<svg width="48px" height="1px" viewBox="0 0 48 1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect id="Rectangle-5" x="25" y="36" width="48" height="1"></rect></svg>';

    return (
        <Image source={{
                          uri: "data:image/svg+xml;utf8," + mySVGImage
                      }}/>
    );
  }
}

Option 3 (coolest option)

It isn't free like the two other options, but it isn't expensive either. PaintCode is one of those tools that I just love. Version 3+ now supports java-script. So you could load your svg files in it, and it will spit out code that you can use in your app. The advantage here that the interface is friendlier than the first option above, and you can then animate each object inside the svg file from your code.

like image 95
Francois Nadeau Avatar answered Oct 02 '22 16:10

Francois Nadeau