Search code examples
react-nativereact-native-firebase

how to render data fecthed from firebase database in react-native functional components


I'm getting data from firebase databse in console that look like this. [{ "isDonor": true, "name": "Nadi", "photo": "https://gre", "uid": "2ZE" }, { "email": "mmaz", "isDonor": true, "name": "Mz", "photo": "https://gra", "uid": "Cb" }] I want to create cards of each objects but how to accomplish that as data is fetched after some time? I want to render it like this image

Ihave checked other answres but they are mostly from class component. I have tried using useEffect hook but couldn't implement it here is my code

import * as React from 'react';
import {Text, View, StyleSheet, Image} from 'react-native';
import database from '@react-native-firebase/database';

const donorsData = [];
database()
  .ref('users')
  .orderByChild('isDonor')
  .equalTo(true)
  .once('value')
  .then((results) => {
    results.forEach((snapshot) => {
      // console.log(snapshot.key, snapshot.val());
      //   console.log(snapshot.val());
      donorsData.push(snapshot.val());
    });
    //   console.log('aft', donorsData);
  });
export default function New() {
  return (
    <View style={styles.container}>
      {donorsData.map((v, i) => {
        return (
          <View
            key={v.uid}
            style={{
              backgroundColor: 'white',
              padding: 10,
              margin: 5,
              borderRadius: 10,
            }}>
            <Text>{v.name}</Text>
            <Text>{v.email}</Text>
            <Image source={{uri: v.photo}} style={{height: 150, flex: 1}} />
          </View>
        );
      })}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ecf0f1',
    padding: 8,
    backgroundColor: 'lightblue',
  },
});

Solution

  • 
    import React, { useState, useEffect } from "react";
    import { Text, View, StyleSheet, Image } from "react-native";
    import database from "@react-native-firebase/database";
    
    export default function New() {
      const [data, setData] = useState([]);
    
      useEffect(() => {
        const donorsData = [];
        database()
          .ref("users")
          .orderByChild("isDonor")
          .equalTo(true)
          .once("value")
          .then((results) => {
            results.forEach((snapshot) => {
              donorsData.push(snapshot.val());
            });
            setData(donorsData);
          });
      }, []);
    
      return (
        <View style={styles.container}>
          {data?.map((v, i) => {
            return (
              <View
                key={v.uid}
                style={{
                  backgroundColor: "white",
                  padding: 10,
                  margin: 5,
                  borderRadius: 10,
                }}
              >
                <Text>{v.name}</Text>
                <Text>{v.email}</Text>
                { v.photo && <Image source={{ uri: v.photo }} style={{ height: 150, flex: 1 }} />} 
              </View>
            );
          })}
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: "#ecf0f1",
        padding: 8,
        backgroundColor: "lightblue",
      },
    });