【发布时间】:2021-02-05 10:40:46
【问题描述】:
我正在从控制台中的 firebase 数据库获取数据,如下所示。 [{ "isDonor": true, "name": "Nadi", "photo": "https://gre", "uid": "2ZE" }, { "email": "mmaz", "isDonor": true, "name": "Mz", "photo": "https://gra", "uid": "Cb" }]
我想为每个对象创建卡片,但是如何在一段时间后获取数据时做到这一点?
我想像这样渲染它
我检查了其他答案,但它们大多来自类组件。
我曾尝试使用useEffect 钩子但无法实现它
这是我的代码
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',
},
});
【问题讨论】:
标签: react-native react-native-android react-native-firebase