【发布时间】:2022-10-19 12:15:40
【问题描述】:
我在 IOS 模拟器上渲染数据有问题。渲染在网站上正常工作,但在 IOS 上我仍然卡在“正在加载..”文本上。
这是我的代码:
import React from 'react'
import { useState } from 'react';
import { useEffect } from 'react';
import { SafeAreaView, Text, View, StyleSheet, Image, Alert } from 'react-native';
import { Card } from 'react-native-paper'
import firebase from 'firebase'
import Button from '../components/Button'
import Background from '../components/Background'
import TopBar from '../components/TopBar'
export default function HomeScreen({ navigation }) {
const [data, setData] = useState([])
const sampleData = [{id:0, title:"One"}, {id:1, title: "Two"}]
useEffect(() =>
{
const donorsData = [];
firebase.database()
.ref("testdb")
.orderByChild("isDonor")
.equalTo(true)
.once("value")
.then((results) => {
results.forEach((snapshot) => {
donorsData.push(snapshot.val());
});
setData(donorsData);
});
}, [])
const card = data.length > 0
? data.map(item =>
{
return <Card key={item.uid} style={{ marginBottom: 20, borderRadius: 10, }}>
<Text>{item.name}</Text>
<Text>{item.description}</Text>
<Image src={item.photo}></Image>
</Card>
})
: <Text>Loading...</Text>
return (
<View style={styles.container}>
{card}
</View>
);
}
在网站上一切正常Website Screen
但是在 IOS 模拟器上我只得到了加载
我尝试了很多在这里找到的解决方案,但没有人适用于这种情况。我想可能是因为iOS没有数据?当我将控制台日志放在返回的顶部时,我什么也没得到。
【问题讨论】:
-
请问你为什么要把
setData()放在setTimeout()里面?它应该放在firebase.database()中的then()内。因此,在获取完成之前,空数组不会插入状态。 -
我想我需要等待 IOS 来获取数据显示。因此,如果我使用 useEffect(() => { const donorsData = []; firebase.database() .ref("treninkove_plany") .orderByChild("isDonor") .equalTo(true) .once("value") .then ((results) => { results.forEach((snapshot) => {donorsData.push(snapshot.val()); }); setData(donorsData); }); }, []) 状态相同:(。
标签: ios react-native