Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use svg images on react-native?

I'm trying to use svg images on my native react application, I'm developing on Android.

So I followed this tutorial =>

https://medium.com/faun/add-custom-svg-icons-to-your-expo-app-279b492f6a15

I have an error Unable to read the 'fill' property of undefined while I manage to display the image, so I try to downgrade the version of react-native- svg and the image is displayed but as soon as I integrated react-navigation my application expo on crash at startup.

So I looked for a long time for the cause of this crash.

I tried to delete the react-native-svg library, the metroconfig.js file, and expo worked again, I don't know if this was the cause of the problem.

I would like to know if people have encountered these problems or if not what is the best method which works with the current version of RN to import a svg image in a react-native application?

Thank you in advance for your help and your answers.

EDIT

I tested react-native-svg and react-native-transformer-svg with the latest version of react native / expo / sdk expo

From the moment I create the metro.config.js file and link it with expo by updating the app.json file, my expo application crashes at startup.

I had to use react-native-svg without react-native-transformer-svg, that is to say that I have to convert an SVG file into a reactable SVG file.

If someone has a working solution to import svg files automatically, it would be of great help to me.

like image 271
CallMeMrHyde Avatar asked Feb 15 '20 11:02

CallMeMrHyde


People also ask

How do I use SVG 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.

How do I display SVG images in react?

There are a few ways to use an SVG in a React app: Use it as a regular image. Import it as a component via bundler magic (SVGR) Include it directly as JSX.

Should I use SVG or PNG in react-native?

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

Important Note - I develop on a real Android device, not in Expo!

Here is some code from an issue opened on Github that actually worked for me after some modification.

Link to Github issue

In my metro.config.js file I finally have this code:

const { getDefaultConfig } = require("metro-config")

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig()

  // here I extend the extensions needed for RN because I use JSX.
  // you don't need this if you use pure JS files
  const updatedSourceExts = [...sourceExts, "jsx", "js", "json", "ts", "tsx"]

  return {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: false,
        },
      }),
      babelTransformerPath: require.resolve("react-native-svg-transformer"),
    },
    resolver: {
      assetExts: assetExts.filter((ext) => ext !== "svg"),
      sourceExts: [...updatedSourceExts, "svg"],
    },
  }
})()

A part of my package-json file:

  • "react-native": "0.63.2",
  • "react-native-svg": "^12.1.0",
  • "react-native-svg-transformer": "^0.14.3"
like image 137
tonkatata Avatar answered Sep 23 '22 23:09

tonkatata