Introduction
Here is an Example of Grid Image Gallery in React Native. We will use FastImage
component from react-native-fast-image
for our Grid Image Gallery in React Native. If you have no idea about FastImage then you can get the idea about it from Fast Speed Image Loading using React Native Fast Image.
An image Gallery is the most common thing in an application. To make an Image gallery in React Native we have a number of options available and we are using one of them in this example. You can also see our other Example of Slider Image Gallery in React Native.
FastImage Component
<FastImage
style={styles.image}
source={{
uri: 'https://unsplash.it/400/400?image=1',
headers: { Authorization: '9876543210' },
}}
/>
It is a very easy-to-use component. We are going to make a grid of images using Flat list (If you want to get an idea of how to do that then you can see Example of GridView using FlatList in React Native) and after clicking on the image from the grid view we are going to open it in modal component for the full view. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use FastImage
component we need to install react-native-fast-image
dependency.
To install these dependencies open the terminal and jump into your project using
cd ProjectName
Run the following commands
npm install react-native-fast-image --save
This command will copy all the dependencies into your node_module directory.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Code
Now Open App.js in any code editor and replace the code with the following code
App.js
// Example of Grid Image Gallery in React Native
// https://aboutreact.com/grid-image-gallery/
// import React in our code
import React, {useState, useEffect} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
Text,
View,
TouchableOpacity,
FlatList,
Modal,
} from 'react-native';
//import FastImage
import FastImage from 'react-native-fast-image';
const App = () => {
const [imageuri, setImageuri] = useState('');
const [
modalVisibleStatus, setModalVisibleStatus
] = useState(false);
const [dataSource, setDataSource] = useState([]);
useEffect(() => {
let items = Array.apply(null, Array(120)).map((v, i) => {
return {
id: i,
src: 'https://unsplash.it/400/400?image=' + (i + 1)
};
});
setDataSource(items);
}, []);
const showModalFunction = (visible, imageURL) => {
//handler to handle the click on image of Grid
//and close button on modal
setImageuri(imageURL);
setModalVisibleStatus(visible);
};
return (
<SafeAreaView style={styles.container}>
{modalVisibleStatus ? (
<Modal
transparent={false}
animationType={'fade'}
visible={modalVisibleStatus}
onRequestClose={() => {
showModalFunction(!modalVisibleStatus, '');
}}>
<View style={styles.modelStyle}>
<FastImage
style={styles.fullImageStyle}
source={{uri: imageuri}}
resizeMode={
FastImage.resizeMode.contain
}
/>
<TouchableOpacity
activeOpacity={0.5}
style={styles.closeButtonStyle}
onPress={() => {
showModalFunction(!modalVisibleStatus, '');
}}>
<FastImage
source={{
uri:
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/close.png',
}}
style={{width: 35, height: 35}}
/>
</TouchableOpacity>
</View>
</Modal>
) : (
<View style={styles.container}>
<Text style={styles.titleStyle}>
Grid Image Gallery in React Native
</Text>
<FlatList
data={dataSource}
renderItem={({item}) => (
<View style={styles.imageContainerStyle}>
<TouchableOpacity
key={item.id}
style={{flex: 1}}
onPress={() => {
showModalFunction(true, item.src);
}}>
<FastImage
style={styles.imageStyle}
source={{
uri: item.src,
}}
/>
</TouchableOpacity>
</View>
)}
//Setting the number of column
numColumns={3}
keyExtractor={(item, index) => index.toString()}
/>
</View>
)}
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
},
titleStyle: {
padding: 16,
fontSize: 20,
color: 'white',
backgroundColor: 'green',
},
imageContainerStyle: {
flex: 1,
flexDirection: 'column',
margin: 1,
},
imageStyle: {
height: 120,
width: '100%',
},
fullImageStyle: {
justifyContent: 'center',
alignItems: 'center',
height: '100%',
width: '98%',
resizeMode: 'contain',
},
modelStyle: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.4)',
},
closeButtonStyle: {
width: 25,
height: 25,
top: 50,
right: 20,
position: 'absolute',
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
This is how you can make Grid Image Gallery in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Hello Snehal
Greetings for the day!
How can I add the swipe(left & right) functionality into this?
Sincerely
Hardik
You can add this https://aboutreact.com/example-of-slider-image-gallery-in-react-native/ instead of modal
I want to build something similar but, instead of images, each square should contain another react component. Coincidentally, part of the component I want to display is an image but also there is text information so, ideally, just a responsive grid of components. Any suggestions on ideas of how to start?
I hope this will help you https://aboutreact.com/view-like-google-play-store/