How to pass dynamically fetched image array data in react native grid image viewer with caption in react native

Viewed 476

I am using the react-native-grid-image-viewer-with-caption plugin to display the image gallery on click it converts to slider.

Below is the code for same:

import React, { Component } from 'react';
import {useContext, useState, useEffect} from 'react';
import {
  View,
  Text,
  StyleSheet,
  Image,
  TouchableOpacity,
  ScrollView,
  ImageBackground,
  Platform,
  Linking,Share,
  ActivityIndicator, Button, TextInput, Touchable
} from 'react-native';
import GridImageViewerCaption from 'react-native-grid-image-viewer-with-caption';


const TestScreen = ({ route, navigation }) => {
  const [imageList, setImageList] = useState([]);
  
  useEffect(() => {
    fetchimages();
    const gallery = [
       { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 1'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 2'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 3'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 4'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 5'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 6'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 7'},
        { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 8'}
        
    ];
    
  }
  const fetchimages = async () => {
    setImageLoading(true)
    var InsertAPIFETCH = 'https://testapi.com/images';
    var headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    };
    var Data = {
      id: route.params.id,
    };
    fetch(InsertAPIFETCH, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(Data),
    })
      .then(response => response.json())
      .then(responseJson => {
        setImageList(responseJson.imageres);
        
      })
      .catch(error => {
        alert(error);
      });
  }
 
  if (isLoading == true) {
    return <LottieView
    source={require('../assets/indicator.json')}
    autoPlay
    loop={true}
    speed={0.8}
    style={{ justifyContent: "center", flex: 1 }}
    />
  } else {
    return (
      <View style={styles.container}>
        <GridImageViewerCaption data={gallery} captionColor="#fff" />      
      </View> 
    );
  }
}

export default TestScreen;```

I want to pass the API JSON response to the gallery function which intern will be passed to GridImageViewerCaption. More detailed elaboration is shown in the images attached.

Can anybody pls help me with this RefLink: https://github.com/ansh-099/react-native-grid-image-viewer

Thanks in advance.

enter image description here

1 Answers

make gallery variable as default value of imageList and use imageList as data in GridImageViewerCaption

  const gallery = [
   { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 1'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 2'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 3'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 4'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 5'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 6'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 7'},
    { image: 'https://via.placeholder.com/300/09f.png', text: 'Caption 8'}
];

  const [imageList, setImageList] = useState(gallery);

  <GridImageViewerCaption data={imageList} captionColor="#fff" />      
Related