简体   繁体   English

在 React Native 中使用 Firebase(Firestore) 列出数据时导致空白屏幕

[英]Resulting in a blank screen when listing data with Firebase(Firestore) in React Native

Data listing process with Firebase(Firestore) in React Native.在 React Native 中使用 Firebase(Firestore) 列出数据的过程。

In the video I watched, he wrote and ran the code that lists the coins belonging to the user using firestore.在我观看的视频中,他编写并运行了列出属于使用 firestore 的用户的硬币的代码。 It did not work even though I wrote the same codes.即使我编写了相同的代码,它也不起作用。 What is the reason?是什么原因? the code does not give an error, the screen appears, but it does not list.该代码没有给出错误,屏幕出现,但它没有列出。
The problem is that when I did the listing in console it worked.问题是当我在控制台中列出它时它起作用了。 However, when I try to list with FlatList, the screen appears blank.但是,当我尝试使用 FlatList 列出时,屏幕显示为空白。

import { View, Text, SafeAreaView, TouchableOpacity, FlatList } from 'react-native'
import React, { useContext, useState, useEffect } from 'react'

import IconFA5 from 'react-native-vector-icons/FontAwesome5'
import IconFA from 'react-native-vector-icons/FontAwesome'

import { deviceWidth, deviceHeight } from '../../utils/dimensions'

import { AuthContext } from '../../navigation/AuthProvider'
import { Formik, validateYupSchema } from 'formik'
import * as yup from 'yup'
import { firebase } from '@react-native-firebase/auth'
import auth from '@react-native-firebase/auth'
import firestore from '@react-native-firebase/firestore'

const color = '#aaa';

const HomeScreen = ({ navigation }) => {

  const renderItem = (item) => {
    <TouchableOpacity
      key={item.id}
      style={{
        flexDirection: 'row',
        width: '95%',
        height: 60,
        borderWidth: 1,
        margin: 10,
        borderRadius: 20,
        padding: 10,
        justifyContent: 'space-between',
        alignItems: 'center'
      }}>

      <View style={{ flex: 1 }}>
        <Text style={{
          textAlign: 'left',
          fontSize: 18
        }}>{item.coinID}</Text>
      </View>

      <View style={{ flex: 1 }}>
        <Text style={{
          textAlign: 'right',
          fontSize: 18
        }}>{item.value}</Text>
      </View>



    </TouchableOpacity>
  }

  const { signOut, user } = useContext(AuthContext);
  const [currentUser, setCurrentUser] = useState({});
  const [userCoinList, setUserCoinList] = useState([]);

  const usersColl = firestore().collection('users');
  const coinsColl = firestore().collection('coins');
  const userCoinsColl = firestore().collection('userCoins');

  useEffect(() => {

    return usersColl
      .doc(user.uid)
      .get()
      .then(result => {
        setCurrentUser(result.data());

        userCoinsColl.onSnapshot((querySnapshot) => {
          let list = [];
          querySnapshot.forEach(doc => {
            const { userID, coinID, value } = doc.data();

            if (userID == user.uid) {
              list.push({
                id: doc.id,
                userID,
                coinID,
                value
              });
              setUserCoinList(list);
            }

          });
        });

      });

  }, []);

  return (
    <SafeAreaView style={{ width: '100%', height: '100%' }}>
       <View style={{
        width: '100%',
        height: '90%',
        padding: 5,
        flex: 1,
        alignItems: 'center'
      }}>

         <View>

          <FlatList
            style={{ flex: 3, backgroundColor: '#f00' }}
            data={userCoinList}
            keyExtractor={item => item.id}
            renderItem={renderItem}
          />
          {console.log(userCoinList)}
        </View>


      </View>

    </SafeAreaView>
  )
}

export default HomeScreen

Your renderItem function does not return anything.您的renderItem函数不返回任何内容。 Here is a fix.这是一个修复。

const renderItem = (item) => {
    return <TouchableOpacity
      key={item.id}
      style={{
        flexDirection: 'row',
        width: '95%',
        height: 60,
        borderWidth: 1,
        margin: 10,
        borderRadius: 20,
        padding: 10,
        justifyContent: 'space-between',
        alignItems: 'center'
      }}>

      <View style={{ flex: 1 }}>
        <Text style={{
          textAlign: 'left',
          fontSize: 18
        }}>{item.coinID}</Text>
      </View>

      <View style={{ flex: 1 }}>
        <Text style={{
          textAlign: 'right',
          fontSize: 18
        }}>{item.value}</Text>
      </View>



    </TouchableOpacity>
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM