React native snippet. Image Grid Layout

This react native snippet, Image Grid Layout , was created to help android and ios developers save time
while they build native mobile apps using JavaScript and React
use it in your project and build your app faster

React native code

Copy, paste and run the following code to get a result like the one shown in the image

import React from 'react';
import { 
  StyleSheet, 
  Text, 
  View, 
  FlatList, 
  Dimensions,
  TouchableOpacity,
  Image,
  Modal,
  ScrollView
} from 'react-native';

export default class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      numColumns:3,
      userSelected:{},
      modalVisible:false,
      data: [
        {id:1,  image:"https://bootdey.com/img/Content/avatar/avatar1.png"}, 
        {id:2,  image:"https://bootdey.com/img/Content/avatar/avatar2.png"}, 
        {id:3,  image:"https://bootdey.com/img/Content/avatar/avatar3.png"}, 
        {id:4,  image:"https://bootdey.com/img/Content/avatar/avatar4.png"}, 
        {id:5,  image:"https://bootdey.com/img/Content/avatar/avatar5.png"}, 
        {id:6,  image:"https://bootdey.com/img/Content/avatar/avatar6.png"}, 
        {id:7,  image:"https://bootdey.com/img/Content/avatar/avatar7.png"}, 
        {id:8,  image:"https://bootdey.com/img/Content/avatar/avatar1.png"}, 
        {id:9,  image:"https://bootdey.com/img/Content/avatar/avatar2.png"}, 
        {id:10, image:"https://bootdey.com/img/Content/avatar/avatar3.png"},
      ]
    };
  }

  clickEventListener = (item) => {
    this.setState({userSelected: item}, () =>{
      this.setModalVisible(true);
    });
  }

  setModalVisible(visible) {
    this.setState({modalVisible: visible});
  }

  renderItem = ({item, index}) => {
    if (item.empty === true) {
      return <View style={[styles.item, styles.itemInvisible]} />;
    }
    var itemDimension = Dimensions.get('window').width / this.state.numColumns;
    return (
      <TouchableOpacity style={[styles.item, {height: itemDimension}]} onPress={() => {this.clickEventListener(item)}}>
        <Image style={{height:itemDimension - 2, width:itemDimension - 2}} source={{uri: item.image}}/>
      </TouchableOpacity>
    );
  }
  
  formatRow = (data, numColumns) => {
    const numberOfFullRows = Math.floor(data.length / numColumns);
    let numberOfElementsLastRow = data.length - (numberOfFullRows * numColumns);
    while (numberOfElementsLastRow !== numColumns && numberOfElementsLastRow !== 0) {
      data.push({ id: `blank-${numberOfElementsLastRow}`, empty: true });
      numberOfElementsLastRow++;
    }
    return data;
  }

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          data={this.formatRow(this.state.data, this.state.numColumns)}
          keyExtractor= {(item) => {
            return item.id;
          }}
          renderItem={this.renderItem}
          numColumns={this.state.numColumns}/>

        <Modal
          animationType={'fade'}
          transparent={true}
          onRequestClose={() => this.setModalVisible(false)}
          visible={this.state.modalVisible}>

          <View style={styles.popupOverlay}>
            <View style={styles.popup}>
              <View style={styles.popupContent}>
                <ScrollView contentContainerStyle={styles.modalInfo}>
                  <Image style={{width:200, height:200}} source={{uri: this.state.userSelected.image}}/>
                </ScrollView>
              </View>
              <View style={styles.popupButtons}>
                <TouchableOpacity onPress={() => {this.setModalVisible(false) }} style={styles.btnClose}>
                  <Text style={styles.txtClose}>Close</Text>
                </TouchableOpacity>
              </View>
            </View>
          </View>
        </Modal>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginVertical: 20,
  },
  item: {
    backgroundColor: '#ffffff',
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1,
  },
  itemInvisible: {
    backgroundColor: 'transparent',
  },

 /************ modals ************/
  popup: {
    backgroundColor: 'white',
    marginTop: 80,
    marginHorizontal: 20,
    borderRadius: 7,
  },
  popupOverlay: {
    backgroundColor: "#00000057",
    flex: 1,
    marginTop: 20
  },
  popupContent: {
    //alignItems: 'center',
    margin: 5,
    height:250,
  },
  popupHeader: {
    marginBottom: 45
  },
  popupButtons: {
    marginTop: 15,
    flexDirection: 'row',
    borderTopWidth: 1,
    borderColor: "#eee",
    justifyContent:'center'
  },
  popupButton: {
    flex: 1,
    marginVertical: 16
  },
  btnClose:{
    height:20,
    backgroundColor:'#20b2aa',
    padding:20
  },
  modalInfo:{
    alignItems:'center',
    justifyContent:'center',
  }
}); 

About this react-native snippet

This react-native snippet, Image Grid Layout , was published on Dec 27th 2018, 10:57 by Dey Dey and it is free.

We hope you will enjoy this awesome react-native snippet and stay tuned for the latest updates, we believe it will save your precious time and gives trendy look to your next application.

This snippet currectly have 1.7K views, Using this react-native snippet you have the following benefits:

Example Screens

You can jump start your development with our pre-built example, all you need to do is copy the code and execute it, You can also show it to your customers so that they have an idea of the final project.

Cross-platform for mobile development

This snippet was developed for Android and iOS.

Simple Integration

This snippet can be simply integrated on existing projects and new ones too, all you need to do is copy the code and start working.

Fully coded view

this is a functional example, You will save a lot of time going from prototyping to full-functional code.