React-Native:如何使用异步函数获取平面列表的数据
我对原生反应还很陌生,最近一直在开发时间表应用程序。我试图从数据库获取数据并将其显示在平面列表中,但是平面列表始终显示为空。 我不太确定我做错了。当我将数据记录到控制台时,它会将正确的数据记录到控制台,但是当我尝试使平面列表呈现数据时,它显示为空
timesheets.js
export default function TimesheetScreen({ navigation }) {
const getData = async () => {
data = await getTimesheets()
console.log(data);
return data;
}
const timesheetsData = getData();
const Item = ({ name }) => (
<View style={styles.body}>
<Text styles={styles.bodyText}>{name}</Text>
</View>
);
const renderItem = ({ item }) => (
<TouchableOpacity onPress={() =>navigation.navigate('DetailScreen', item)}>
<Item name={item.name +": " + item.date}/>
</TouchableOpacity>
);
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>My Timesheets</Text>
</View>
<TextInput
style={styles.searchBackground}
placeholder='Enter Employee Name'
onEndEditing={text=>console.log(text)}/>
<FlatList
data={timesheetsData}
renderItem={renderItem}
keyExtractor={item => item.clockID}
/>
</View>
);
}
正在查询数据库的函数
export const getTimesheets = async () =>{
//gets all employee records from collection employees
var allTimesheets= await firebase.firestore()
.collection('employees')
.get();
var timesheetsArray=[];
for(let i=0;i<(allTimesheets.docs).length;i++){
let timesheetsData=(allTimesheets.docs[i]).data();
timesheetsArray.push(timesheetsData);
}
谢谢
I'm pretty new to react native, and have been working on a timesheets app recently. I'm trying to get data from a database and display it in a flatlist, however the flatlist keeps appearing empty.
I'm not really sure I'm doing wrong. When I log the data to the console it logs the correct data to the console but when I try to make the flatlist render the data it appears empty
timesheets.js
export default function TimesheetScreen({ navigation }) {
const getData = async () => {
data = await getTimesheets()
console.log(data);
return data;
}
const timesheetsData = getData();
const Item = ({ name }) => (
<View style={styles.body}>
<Text styles={styles.bodyText}>{name}</Text>
</View>
);
const renderItem = ({ item }) => (
<TouchableOpacity onPress={() =>navigation.navigate('DetailScreen', item)}>
<Item name={item.name +": " + item.date}/>
</TouchableOpacity>
);
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>My Timesheets</Text>
</View>
<TextInput
style={styles.searchBackground}
placeholder='Enter Employee Name'
onEndEditing={text=>console.log(text)}/>
<FlatList
data={timesheetsData}
renderItem={renderItem}
keyExtractor={item => item.clockID}
/>
</View>
);
}
the function that is querying the database
export const getTimesheets = async () =>{
//gets all employee records from collection employees
var allTimesheets= await firebase.firestore()
.collection('employees')
.get();
var timesheetsArray=[];
for(let i=0;i<(allTimesheets.docs).length;i++){
let timesheetsData=(allTimesheets.docs[i]).data();
timesheetsArray.push(timesheetsData);
}
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
getData
是异步的,常量timesheetsData
在第一个渲染周期中不包含任何内容,并且当数据发生变化时您不会触发state
准备就绪,不会触发重新渲染,从而导致FlatList
为空。这可以通过使用 useEffect 并引入状态变量来解决,如下所示。
Since
getData
is async, the constanttimesheetsData
does not contain anything in the first render cycle and since you are not triggering astate
change when the data is ready, no rerender is triggered, resulting in an emptyFlatList
.This could be solved by using a
useEffect
and introducing a state variable as follows.我注意到你缺少状态。
在 React 中,每次状态更改都会使用新的状态数据重新渲染 UI。
获取数据库结果后,您必须使用状态变量重新渲染屏幕。
这可以通过
React.useState()
钩子函数来解决。所以,请尝试一下这个方法。
I have noticed that you are missing state.
In React, every time state is changed makes UI re-render with new state data.
After getting database result, you have to re-render screen with state variable.
This can be solved by
React.useState()
hook function.So, please try this way.