How to Create image Component in React Native

In this quick tutorial, you will learn how to add diffrent types of images in React Native, how to show and use network images, how to show and use static images from the assets directory, and how to show and use local images.

Also, we will certainly show you how to create the image component in react-native from absolute scratch.

A React component is needed for showcasing different images. This react native image component example will help you learn how to show network images, static resources, temporary local images, and images from local disks, such as the camera roll.

This example will take the help of Image module and teach you how to fetch and display an image from local app storage and from the network, and even from base64 image using the data uri.

React Native Image Component Example

  • Step 1: Install React Native CLI
  • Step 2: Create React Native App
  • Step 3: Create Image Component
  • Step 4: Register Component in App Js
  • Step 5: Run Development Server

Install React Native CLI

This tutorial is based on expo cli, we assume you have already installed the tool, if not install the Node 12 LTS or higher in your development system using the given command.

Run any of the command (npm or yarn) to begin installing the Expo CLI.

# npm
npm install -g expo-cli

# yarn
yarn global add expo-cli

Create React Native App

Now, you may use the suggested command and begin the installation of the new React Native application. However, you may jump onto the subsequent step if the app is already installed.

expo init RnBlogProject

Further, step inside the app directory.

cd RnBlogProject

Build Image Component

Let us start building image component systematically, for that, and you have to create a new image_component.js file; this file should be created within the reactive native app directory.

Now, update code the in image_component.js file.

import React, { Component } from 'react';

export default class ImageComponent extends Component {
  
};

Display Static Image in React Native

You have to import View, and Image submodules from the main ‘react-native’ module, wrap the image around the View, define the source, pass require tag, and call the image from the project’s asset folder. Make sure to keep the image in the assets directory.

import React, { Component } from 'react';
import {  View, Image } from 'react-native'

export default class ImageComponent extends Component {
	render() {
		return (
		  <View>
			<Image 
			  style={{ width: 150, height: 150 }}
			  source={require('./assets/user.png')}
			/>
		  </View>
		);
	  }
};

Show Network Image

You can easily display images from the server, and you have to pass the source tag, define the uri and add the image path.

import React, { Component } from 'react';
import {  View, Image } from 'react-native'

export default class ImageComponent extends Component {
	render() {
		return (
		  <View>
			<Image 
			  style={{ width: 150, height: 150 }}
			  source={{uri: 'https://www.remotestack.io/wp-content/uploads/2021/09/5273-01.png'}}
			/>
		  </View>
		);
	  }
};

Display Base64 Image

On the other hand, showing a base64 image with data uri is possible in react native; simply add the base64 code in the source attribute.

import React, { Component } from 'react';
import {  View, Image } from 'react-native'

export default class ImageComponent extends Component {
	render() {
		return (
		  <View>
			<Image 
			  style={{ width: 150, height: 150 }}
			  source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAADAFBMVEXv7+/w8PDu7u5dzH8ibDi56cf///8gaDayyLkeaTRfzYFf04I7qVwjbTkhajfB7M1ezYCbuaRj1YUfazYdZTNFq2TA7My968oWYi0caDK76sjH89NUyXggaTYdYzKy58IfZjQZZDC+6su66slVynlby30aZzETYStZy3wdZzRh04S36MVXynq26MRYynu46cYPYCdbzH7r7Otg0IIYcTMKXCPU3da/885TyXcNXSXE8dGx5sBwvYdPyHQ0eEgiczocYjHJ18ym4bdh0YMocD0SXimQ26Zh1oVYy3vI9dW3y72h4rS868m06MM8fE+c369/xpNs0IpkzYQXZS78/v3l9+re8+OtxbR51JRsm3svdUQmeD4LXST3/PjW896I2aCC15t3ooRll3RYjmgSZCu88MvA0cWV3KqVtp85ekwhbzj4/vrq+e7t7e3P79jD9NLN2dHD7M/B7c5d0YBHs2hFglckbjrz/fXa8uGmwK6eyqmEq497pYhyn4AkdjwNYyff5ODK7tWq5LqgvamN2qOGrJJQimE+q2AykU4JYCTw+PLT8dvY4NqQs5tbz39QwHNfk24/gFIth0gnf0IaYC/k6OV/1Jdnz4ZWx3hNvG83nFU2hEwteUMne0Dn6ejg9uba4t3N59K+7Mu8zsK32r+u5b6r0bSS1aWYuKKPxJ510pBLuGxakWpQoWhUl2ZIll47llYkfD0TbC7S6tjB786/7s3E08mX3ayOvJqEyJeMsJZw0o1hlXFZpG5VjGZGjFo5oVgdcTYabTK0ybqa1quM0J9sqHxerXRLxnE9pl1KhVs2lVMwjEwqg0UaaDHv/PLb7N/G4My27saz7MOx1bqh3LGBqY1fz4FhoHIugEUrckAebTXo/u7a9eLJ5s+pwrGgxql5wI5juHxnpHdVw3Zbm2xOkmA8jVMZdjbO9tq93MOo6b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WjXinzzu19th3/R9w0HAfflIwGVBASUAIhHHWq8vRU5NPIuZ7xzrx3r1vcsAijhXgC+PgRoSSvj45wzPnZ4S/FFJJwLiHzAthe8NaHFQU4JdAAQD0Ry3wXGw/+I8QRyQGod/G+oSyUggFp99Lb/CUdXU9cH/9kcL4AXwAvgBVBtGl4AL4AXQLVpeAG8AF4A1abhBfACeAFUm4YXwAvgBVBtGl4AL4AXQLVp/m6HDggAgGEYhH31L/pCCA4IAAAAlg4AAABLBwAAgKUDAADA0tX/t3cv3X3B4t8M3EU+XwAAAABJRU5ErkJggg=='}}
			/>
		  </View>
		);
	  }
};

Register Component in App Js

Ideally, you must visit the main app js file, in this file you should register the ImageComponent in the primary app js file.

Open the App.js file; and add the following code.


import React from 'react';

import ImageComponent from './image_component.js'


export default function App() {
  return (
    <ImageComponent/>
  );
}

Run Development Server

You have to test the react native app; hence, install the Expo Client on your smartphone. You can get the expo client either from Google Play Store or App Store.

Next, scan the visible QR code but before that make sure to run either of the command.

# npm
npm start

# yarn
yarn start

Wait for some time while your project is being compiled. Ensure that your development machine and smartphone are on the same network; you will see your project on your smartphone if everything goes well.

How to Create Image Component in React Native

Summary

In this guide, we learned how to display different images in React Native. We looked at how to use image and View module, and showed static image, data uri image and network images in react native image component.