Black Friday Sale Upgrade Your Home →

Display Local and Remote Images

Display Local and Remote Images in React Native

Images in React Native can be displayed with the built in Image component, which is similar the the img tag on the web. We’ll use Image to show both local images (included in the source of the app), and remote images (fetched from a web address).

High resolution images

We can specify higher resolution images for higher density screens by appending @2x or @3x to the image name.

BASH
.
├── coffee.png
├── coffee@2x.png
└── coffee@3x.png

When importing the images, the @2x and @3x are excluded from the path, however the extension is required.

JSX
import coffee from '../images/coffee.png'

Displaying images

React Native's <Image /> component is used to display images - similar to the <img /> element in HTML. The source prop is used instead of src to specify the image's path.

When using a local image, the image is passed to the source prop, however, when loading a remote image - such as a URL - you must provide an object to source with a uri property.

Local:

JSX
import { Image } from 'react-native'
import coffee from '../images/coffee.png'
...
<Image source={coffee} />

Remote:

JSX
import { Image } from 'react-native'
...
<Image source={{ uri: 'https://good-coffee.com/coffee.png' }} />

Dimensions must be specified for the <Image /> component to display.

JSX
<Image
source={coffee}
style={styles.img}
/>
const styles = StyleSheet.create({
img: {
width: 200,
height: 200,
},
})

👍If the dimensions aspect ratio is incorrect, the <Image /> component will automatically clip the image. This behavior can be overridden with the resizeMode prop.

JSX
<Image
source={coffee}
style={styles.img}
resizeMode="contain"
/>
const styles = StyleSheet.create({
img: {
width: 200,
height: 100,
},
})

New component

🤔<Image /> - used for displaying images.


🐦   Previous   💼   Next