Advertisement
  1. Code
  2. Mobile Development
  3. React Native Templates

Understanding the Image Component in React Native

Scroll to top

Images are an essential aspect of any mobile app. This tutorial will introduce you to the image component and show you how to use images in your React Native app. You will also learn how to create your own photo gallery!

Prerequisites to Create a React Native App

We will use the Expo CLI for this project. With Expo, developers can create React Native apps without all the frustrations that come with installing and configuring software dependencies such as Android Studio, Xcode, or all the other tools which are needed to develop and run a React Native app. If you want to learn more about Expo, check out our post on how Expo makes React Native app development easier.

If you don't already have Expo CLI, first ensure you have Node.js installed. Then install the Expo CLI (command-line interface) globally on your machine:

1
npm install expo-cli --global

Then, use the expo command to initialize a new project.

1
expo init project_photos

If you need to use images when testing, add them to the assets folder of the project. 

Add Images to a React Native App

To add images in the application, you first need to import the Image component from react-native. The React Native image component allows you to display images from different sources, such as:

  • network images
  • static resources
  • temporary local images
  • local disk images, i.e. from the camera roll

To import the Image component, add it to the import statement at the top of app.js, as shown below.

1
import {  View, Text, Image, StyleSheet } from 'react-native'

Display Static Images

To display a static image, the first thing to add is the image file in the assets folder of the project. Static images are loaded by providing the image path. The code for displaying a static image will look something like this:

1
import { StatusBar } from "expo-status-bar";
2
import React from "react";
3
import { StyleSheet, Text, View, Image} from "react-native";
4
5
6
export default function App() {
7
  return (
8
    <View style={styles.container}>
9
        <StatusBar style="auto" />
10
        <Text>.........................</Text>

11
        
12
        <Image
13
          style={{ width: 100, height: 100, marginBottom: 15 }}
14
          source={require("./assets/facebook.png")}
15
        />

16
       
17
    </View>

18
  );
19
}
20

Here is the result.

Displaying a static image in React NativeDisplaying a static image in React NativeDisplaying a static image in React Native

Displaying Web-Based Images or URI Data Images

Displaying an image from a network or web-based source is similar to displaying a static image. Within the Image component, use the source attribute and set the path of the image in an object with the uri key, as shown below. 

1
import { StatusBar } from "expo-status-bar";
2
import React from "react";
3
import { StyleSheet, Text, View, Image } from "react-native";
4
5
6
export default function App() {
7
  return (
8
    <View style={styles.container}>
9
    
10
        <StatusBar style="auto" />
11
        
12
        <Text>....................</Text>

13
14
        <Image
15
          style={{ width: 100, height: 100 }}
16
          source={{ uri: "https://reactjs.org/logo-og.png" }}
17
        />

18
      
19
    </View>

20
  );
21
}
22

You'll also need to add the dimensions of the image with a style attribute, just like you would in HTML. Here is the final result for both images.


Network image and static imageNetwork image and static imageNetwork image and static image

You can also display images via a data URI, in which case all the image data is actually encoded in the URI. This is only recommended for very small or dynamic images. For a URI-encoded image, you'll provide the image data with a source attribute like source={{  uri:'data:image/png;base64,iVBOR...=='}}

Don't forget that for network and URI-encoded images, you will need to manually specify the dimensions of your image.

Background Images

You can also use an image as the background for your screen. To do so, get a background image of your choice and add it to the assets folder. Next, import the ImageBackground component from react-native as shown below.

1
import { StyleSheet, Text, View, Image, ImageBackground } from 'react-native';

The ImageBackground component wraps and displays a background for whatever elements are inside it. In this case, we will replace the View tag with the ImageBackground tag and wrap it around all the content in the app.

1
export default function App() {
2
  return (
3
    
4
      <ImageBackground
5
        source={require("./assets/back.jpeg")}
6
        style={styles.back_image}
7
      >
8
        <Text>.........................</Text>

9
        <StatusBar style="auto" />
10
        <Image
11
          style={{ width: 100, height: 100, marginBottom: 15 }}
12
          source={require("./assets/facebook.png")}
13
        />

14
        <Text>..............</Text>

15
16
        <Image
17
          style={{ width: 100, height: 100 }}
18
          source={{ uri: "https://reactjs.org/logo-og.png" }}
19
        />

20
      </ImageBackground>

21
    
22
  );
23
}

Create a Photo Gallery

In this section, I'll show you how to display a grid of photos in a FlatList. This component is used to display large quantities of scrollable content and can scroll horizontally or vertically. 

A FlatList uses the renderItem prop to render each item in its input data. The renderItem prop is a function that takes an item from the data array and maps it to a React element. Each item in the data needs a unique id. This is found in item.key by default, though you can specify another way to find or build the id by using the keyExtractor function prop.

We will use useState to append to an array of images. The useState hook can store any type of value: a number, a string, an array, an object, etc. Add the following code to app.js.

1
import React, { useState} from 'react'
2
3
export default function App() {
4
5
  const [images, setimages] = useState([
6
    require('./assets/image.jpg'),
7
    require('./assets/image1.jpg'),
8
    require('./assets/image2.jpg'),
9
    require('./assets/image3.jpg'),
10
    require('./assets/image4.jpg'),
11
    require('./assets/image5.jpg'),
12
    require('./assets/image6.jpg'),
13
    require('./assets/image7.jpg'),
14
    require('./assets/image8.jpg')
15
  ]);
16
  }

Note that you'll need to have these images in the assets folder. 

Next, we'll return a FlatList that renders those images:

1
  return (
2
    <FlatList
3
      data={images}
4
      key={"2"}
5
      numColumns={2}
6
      renderItem={({ item }) => (
7
        <Image
8
          source={item}
9
          style={{
10
            width: 180,
11
            height: 220,
12
            borderWidth: 2,
13
            borderColor: "#c35547",
14
            resizeMode: "contain",
15
            margin: 6,
16
          }}
17
          keyExtractor={(item) => item.id}
18
        />

19
      )}
20
    />

21
  );

We set up the FlatList element to use the images array as its data source. Then we defined a custom render function that takes an item in the images array and generates an Image component to display it. 

Here is the complete photo gallery:

photo galleryphoto galleryphoto gallery

Conclusion

As you have seen, working with images in React Native is very easy!

If you want to jump start your next React Native app, or to learn from a professionally built app, check out the mobile app templates on CodeCanyon. CodeCanyon is an online marketplace that has hundreds of mobile app templates—for Android, iOS, React Native, and Ionic. You can save days, even months, of effort by using one of them.

CodeCanyon mobile app template bestsellersCodeCanyon mobile app template bestsellersCodeCanyon mobile app template bestsellers

If you have trouble deciding which template on CodeCanyon is right for you, these articles should help: 


Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.