Skip to content

Commit

Permalink
Add support images loaded via require()
Browse files Browse the repository at this point in the history
  • Loading branch information
seekshiva committed Dec 9, 2017
1 parent 3d0b2ac commit ad4aeb9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ import Image from 'react-native-remote-svg'
source={{ uri: 'https://example.com/my-pic.svg' }
style={{ width: 200, height: 532}}
/>
```
**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:
```js
import Image from 'react-native-remote-svg'

<Image source={require('./image.svg')} />
```
You can load normal jpg/png images as well
```js
import Image from 'react-native-remote-svg'

<Image
source={{ uri: 'https://example.com/my-other-pic.png' }
Expand Down
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React from 'react';
import { Image } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import SvgImage from './SvgImage';

const MyImage = props =>
props.source && (props.source.uri && props.source.uri.match('.svg')) ? (
<SvgImage {...props} />
) : (
<Image {...props} />
);
const MyImage = props => {
const source = resolveAssetSource(props.source);
if (source && (source.uri && source.uri.match('.svg'))) {
const style = props.style || {};
if (source.__packager_asset && typeof style !== 'number') {
if (!style.width) {
style.width = source.width;
}
if (!style.height) {
style.height = source.height;
}
}
return <SvgImage {...props} source={source} style={style} />;
} else {
return <Image {...props} />;
}
};

export default MyImage;

0 comments on commit ad4aeb9

Please sign in to comment.