如何在 react-native 功能组件中呈现从 firebase 数据库中获取的数据 [英] how to render data fecthed from firebase database in react-native functional components

查看:65
本文介绍了如何在 react-native 功能组件中呈现从 firebase 数据库中获取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在控制台中从 firebase 数据库获取数据,如下所示.[{ "isDonor": true, "name": "Nadi", "photo": "https://gre", "uid": "2ZE";}, { "email": "mmaz", "isDonor": true, "name": "Mz", "photo": "https://gra", "uid": ";Cb"}]我想创建每个对象的卡片,但是如何在一段时间后获取数据时实现它?我想像这样渲染它

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

我已经检查了其他答案,但它们主要来自类组件.我曾尝试使用 useEffect 钩子,但无法实现它这是我的代码

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',
  },
});

推荐答案


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",
  },
});

这篇关于如何在 react-native 功能组件中呈现从 firebase 数据库中获取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆