Coder Social home page Coder Social logo

react-native-remote-svg's Introduction

react-native-remote-svg

Image component that supports svg filetype in React Native.

Installation

Using npm or yarn:

$ yarn add react-native-remote-svg

This library depends on react-native-webview as peerDependency, which means you need to install the webview library and link it to RN project for this to work

$ yarn add react-native-webview # If you don't already have this
$ react-native link

Image component in react-native doesn't yet support svg file type. This package gives you an Image component that supports both svg and png file types.

It doesn't require any native code setup. No need to do any react-native link. Just import and use!

Breaking Changes:

This project follows semantic versioning. Here are the breaking changes:

  • v2.0.0
    • As of React Native 0.57, WebView has been decoupled and moved to a separate project. This update imports WebView from react-native-webview and can only be used in projects using RN 0.57 or later.

Usage:

You need to import Image from this package instead of from react-native

- import { Image } from 'react-native'
+ import Image from 'react-native-remote-svg'

and you can use this Image component like you normally would:

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

<Image
  source={{ uri: 'https://example.com/my-pic.svg' }}
  style={{ width: 200, height: 532 }}
/>;

supports data uri as well:

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

<Image
  source={{
    uri: `data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px"  viewBox="0 0 100 100">
      <ellipse data-custom-shape="ellipse" cx="50" cy="50" rx="50" ry="50" fill="green"  stroke="#00FF00" stroke-width ="2" />
    </svg>`,
  }}
  style={{ width: 100, height: 100 }}
/>;

Note: When you load an image from the internet, you need to specify width/height of the image (default to 100, 100).

When you load a local image, width/height are not mandatory:

<Image source={require('./image.svg')} />

You can load normal jpg/png images as well

<Image
  source={{ uri: 'https://example.com/my-other-pic.png' }
  style={{ width: 100, height: 120}}
/>

Feature parity:

Here are the list of react-native Image features that are supported:

  • source
  • style
  • blurRadius
  • onLayout
  • onLoad
  • onLoadEnd
  • onLoadStart
  • resizeMode
  • onError
  • testID
  • resizeMethod
  • accessibilityLabel
  • accessible
  • capInsets
  • defaultSource
  • onPartialLoad
  • onProgress

The goal is to have full feature parity with react-native's Image and then add this component directly into react-native itself.

Issues

If you find a bug, please file an issue on our issue tracker on GitHub.

react-native-remote-svg's People

Contributors

alejandronanez avatar benhulan avatar ethanneff avatar karanjthakkar avatar krydos avatar kuzkokov avatar ltoussaint avatar manoj-makkuboy avatar oleksandr-dziuban avatar seekshiva avatar skjangid avatar virdesai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-native-remote-svg's Issues

Invariant Violation: requireNativeComponent: "RNCWebView" was not found in the UIManager.

I am developing a react-native project. I have the following packages (related with my question) installed:

"react-native": "0.63.4",
"react-native-remote-svg": "^2.0.6",
"react-native-webview": "^11.2.1",
...

I import the Image component by:
import Image from 'react-native-remote-svg';

I render a remote SVG image by:

<Image
          source={{ uri: 'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/Steps.svg' }}
          style={{ width: winWidth, height: winHeight }}
        />

(where the winWidth and winHeight are the values of my screen width and height)

When I run my app on iOS simulator, I get the following error:

[Tue Feb 09 2021 15:32:54.328]  ERROR    Invariant Violation: requireNativeComponent: "RNCWebView" was not found in the UIManager.

This error is located at:
    in RNCWebView (at WebView.ios.js:260)
    in RCTView (at View.js:34)
    in View (at WebView.ios.js:263)
    in WebView (at SvgImage.js:76)
    in RCTView (at View.js:34)
    in View (at SvgImage.js:75)
    in SvgImage (at react-native-remote-svg/index.js:18)

Getting Error: Can't perform a React state update on an unmounted component

The remote update was working fine but now it throws this error and the images are not loading.My images are stored locally
Can't perform a React state update on an unmounted component:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
in SvgImage (at react-native-remote-svg/index.js:18)

Double tap zoom

I'm not able to disable double tap zoom on Android.
I have tried scrollEnabled={false} but it's not working. Any suggestions?

Android SVG Image Size not respected

Hi,
It works great on IOS. Thanks.

However, on Android the SVG image size is not respected. It displays a larger image.
In my style I've specified the width and height {width:32,height:32}.

The beginning of my svg Image code is the following :

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none" x="0px" y="0px" width="32px" height="32px" viewBox="0 0 32 32"}>

Here you should simply see a cross in the circle, but you can only see the top left of the cross.

capture d ecran 2018-03-12 a 20 17 37

Build release fail

Hi, when i build apk file with Release mode, so not render svg file.Thanks.

Changing the color of vectors

Hi seekshiva,

First, congrats for your awesome job in this library. I think your approach for svg icons is the best and the easiest.

I don't think my question is an issue. However, I can't find a way to change the color of the icon. The default image doesn't have this prop. Maybe it's just my mistake, could you give me a help?

Best Regards!

No images rendered in release APK

I tried this library for an Android app and it rendered svg icons just fine for the debug build.
Then i tried to create a release apk, but it gave me an error 'filename must end with xml or png.'
So I used "android.disableResourceValidation=true" in gradle.properties, and the error messages were gone.

However, when i run my app, I don't see any svg icons, just a blank area.

style property tintColor is not working

I want to set tintColor for active items in my project. Its not working with this library.

Can you tell me if there is an alternative to tintColor in your library

"tintColor: color
Changes the color of all the non-transparent pixels to the tintColor."

svg not rendering on page

I am using remote-svg on RN project. When i use svg there is no error but not rendered image to. are there some xcode optimization ? when i use svg with webview its rendering.

SVG images not getting rendered on Android devices

Hi,

Till yesterday, my code was rendering the SVG images on android as well as ios without fail. But suddenly it stopped doing so on android devices. When i tried to investigate, i found a solution of changing the HEX to rgb. I tried it then and there only but unfortunately it didn't click. Can you people please provide me a valid solution as my production app is still breaking.

iOS physical device shows no SVG images, Android works

The iOS simulator works exactly as intended. However, the SVG images are not shown when loaded on Expo on a physical iOS device. It works on Android however.

I have a project where this error is shown:
https://exp.host/@fullhdpixel/groenteboer

Test it here:
https://expo.io/appetize-simulator?url=https://expo.io/@fullhdpixel/groenteboer

Usage in App

<Product source={Images.peren}/>

Here is the styled component

import styled from 'styled-components'
import normalize from '../Helpers/normalizeSize'

export const Product = styled.Image`
  flex: 1;
  width: null;
  height: ${normalize(130)}px;
  width: 100%;
`

Images.js

peren: require('../Assets/Images/Products/peren.png'),

Rendering text instead of SVG

I've tried to use it to render remote SVG images but instead of it it's rendering text :

text

import Image from 'react-native-remote-svg';
<Image
  source={{
    uri: `data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px"  viewBox="0 0 100 100">
      <ellipse data-custom-shape="ellipse" cx="50" cy="50" rx="50" ry="50" fill="green"  stroke="#00FF00" stroke-width ="2" />
    </svg>`,
  }}
  style={{ width: 100, height: 100 }}
/>;

package.json

{
  "name": "app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "flow": "flow"
  },
  "dependencies": {
    "@react-native-community/netinfo": "^2.0.3",
    "@react-native-community/viewpager": "^1.1.6",
    "babel-eslint": "^10.0.1",
    "bundle": "^2.1.0",
    "eslint": "^5.16.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^3.6.0",
    "eslint-config-prettier-standard": "^2.0.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.17.1",
    "eslint-plugin-jest": "^22.4.1",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-node": "^8.0.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-promise": "^4.1.1",
    "eslint-plugin-react": "^7.12.4",
    "eslint-plugin-react-native": "^3.6.0",
    "eslint-plugin-standard": "^4.0.0",
    "immutable": "^4.0.0-rc.12",
    "lodash": "^4.17.11",
    "prettier": "^1.17.0",
    "prop-types": "^15.7.2",
    "react": "16.8.3",
    "react-native": "0.59.1",
    "react-native-camera": "^2.4.0",
    "react-native-dropdownalert": "^3.10.0",
    "react-native-extra-dimensions-android": "^1.2.5",
    "react-native-flash-message": "^0.1.11",
    "react-native-modal": "^9.0.0",
    "react-native-orientation-locker": "^1.1.5",
    "react-native-qrcode-scanner": "^1.1.2",
    "react-native-remote-svg": "^2.0.3",
    "react-native-scalable-image": "^0.5.0",
    "react-native-size-matters": "^0.1.7",
    "react-native-splash-screen": "^3.2.0",
    "react-native-svg": "^9.4.0",
    "react-native-swiper": "^1.5.14",
    "react-native-user-inactivity": "^0.1.3-next",
    "react-native-webview": "^5.7.0",
    "react-redux": "^6.0.1",
    "redux": "^4.0.1",
    "redux-devtools-extension": "^2.13.8"
  },
  "devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/preset-flow": "^7.0.0",
    "@babel/runtime": "^7.4.3",
    "babel-cli": "^6.26.0",
    "babel-jest": "^24.7.1",
    "babel-preset-flow": "^6.23.0",
    "flow-bin": "^0.96.0",
    "jest": "^24.7.1",
    "metro-react-native-babel-preset": "^0.53.1",
    "react-test-renderer": "16.8.3",
    "remote-redux-devtools": "^0.5.16"
  },
  "jest": {
    "preset": "react-native"
  }
}

What could be the issue here? Is there any conflicts between my packages? I tried to use remote images / local ones and well, just used an example. Same issue all the on all of those.

Color is not changing

Hello, thanku for providing library but i want to change color of svg image un react-native. i am trying to change it with fill: '#222' but not working. Please let me know is this library supporting to change color of svg image or not ??

borderRadius style prop

Hello!

I have an issue with the borderRadius prop. The svg style prop is not using it.
ex:
<Image
style={{borderRadius: 70}}
source={getCountryFlagIcon(this.props.country.toLocaleLowerCase())}
/>
It works when I apply the borderRadius to a parent View but it's having a weird behavior on android. On android, it's rendering the svg with no borderRadius for a second and then apply the style of the parent view. It's the same when I click on the svg, the style of the parent view disappear for a second.
Everything works fine on ios.

Any idea about that?

Thank you

loop images crashed app

Hello, I have a looping listView with small svg images, when its rendering is ok, but when I start scrolling app isn't responsing and broke. Please help me.

image

Latest v2.0.0 issue related to WebView scalesPageToFit and default useWebkit={true} property

Hello, I have found an issue after upgrade to v2.0.0 that we can't use "react-native-remote-svg" anymore because of WebView update for scalesPageToFit and default useWebkit={true} property.

Latest "react-native-webview" package used internally in "react-native-remote-svg" uses useWebkit={true} prop as default, and when we scalesPageToFit={true} it shows the warning that we can't scale svg image to fit when useWebkit is true.

As result we can see a bit broken SVG image, because it is not scaled to fit the WebView.
And we can't pass useWebkit or scalesPageToFit to internal WebView to disable Weblit View on iOS for example.

The best solution is to add scalesPageToFit and useWebkit props to remote svg Image component and pass them to WebView internally. Fo example if we pass:

<Image 
  useWebkit={false} 
  scalesPageToFit={true} 
  source={{ uri: 'data:image/svg+xml;utf8,...' }}
/>

then scaling will work correctly on iOS platform.

I use:
react-native: 0.59.1
react: 16.8.3
react-native-remote-svg: 2.0.0

MacOS: 10.14.3
Xcode: 10.1

Android SVGs not showing w/ Chrome 72

Overnight my react-native Android application stopped showing remote svgs using this lib (iOS is fine). My application was not updated, but other apps were (Chrome from 71 -> 72). This immediately broke my app and this lib.

I tracked this bug down to WebView, and hopefully others can follow along here: react-native-webview/react-native-webview#298

Currently there is no way to downgrade chrome to get it functional again, but identical phones running Chrome 71 do not exhibit this behavior.

Warning on component unmount

Hi, thanks for this library.

This component is working well for me but the only strange thing is that it gives me this warning every time the component unmounts. Is there something that I have to unsubscribe to in componentWillUnmount?

Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
in SvgImage (at index.js:18)
in MyImage (at index.js:71)
in RCTView (at View.js:60)
in View (at index.js:70)
in RCTView (at View.js:60)
in View (at index.js:69)
in RCTScrollContentView (at ScrollView.js:791)
in RCTScrollView (at ScrollView.js:887)
in ScrollView (at index.js:68)
in Login (created by Connect(Login))
in Connect(Login) (at SceneView.js:10)

[Android] SVG rendered as text instead of image

I'm trying to render a remote SVG. I'm on 2.0.6, Android 12 and Chrome is on version 91.0.4472.114.

Here is the snippet I'm using:

<Image
    source={{ uri: 'https://s3.amazonaws.com/cdn.titanvest.com/misc/car-hex.svg' }}
    style={{ width: 100, height: 100 }} //needs the size
/>

Even using inlined svg doesn't work:

<Image
    source={{
        uri: `data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px"  viewBox="0 0 100 100">
        <ellipse data-custom-shape="ellipse" cx="50" cy="50" rx="50" ry="50" fill="green"  stroke="#00FF00" stroke-width ="2" />
        </svg>`,
    }}
    style={{ width: 300, height: 300 }}

All I get is this:
Screenshot 2022-02-03 at 15 32 40

The same piece of code works perfectly on iOS.

How can I fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.