javascript - 为什么在 react native 中使用 flatlist 不会在屏幕上显示 firebase 数据
问题描述
但是当我使用 console.log 显示数据时,它会正确显示 import React,{useState,useEffect,Component} from 'react'; 从 'react-native' 导入 {StyleSheet,View,Text,TextInput,TouchableOpacity, Button, FlatList};从 'react-native-paper' 导入 { DataTable };从'../config/fire'导入{fire};从'react-native-elements'导入{ListItem,SearchBar};函数 Donorlist(){ const[Donors,setDonors]=useState([]); const fetchDonors=async()=>{ const response=fire.firebase_.firestore().collection('Donors'); 常量数据=等待响应.get(); data.docs.forEach(item=>{ setDonors([...Donors,item.data()]); //console.log("hI I AM IN DONORS FETCH LIST"); })
}
useEffect(()=>{
fetchDonors();
},[])
return (
<View>
<FlatList
data={Donors}
renderItem={({item})=>(
console.log(item.name),
<Text>
{item.name}
</Text> )}/>
{
/*
Donors.map((Donor,index)=>{
return(
console.log(Donor),
<View className="blog-container">
<Text>Donor List</Text>
<DataTable>
<DataTable.Header>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >Name</DataTable.Title>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >Email</DataTable.Title>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >Phone</DataTable.Title>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >Country</DataTable.Title>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >State</DataTable.Title>
<DataTable.Title >City</DataTable.Title>
<DataTable.Title
style={{
width: 100, height: 100, backgroundColor: 'powderblue'
}} >Pin</DataTable.Title>
</DataTable.Header>
<DataTable.Row>
<DataTable.Cell ><Text>{Donor.name}</Text></DataTable.Cell>
<DataTable.Cell >{Donor.email}</DataTable.Cell>
<DataTable.Cell >{Donor.phone}</DataTable.Cell>
<DataTable.Cell >{Donor.name}</DataTable.Cell>
<DataTable.Cell >{Donor.email}</DataTable.Cell>
<DataTable.Cell >{Donor.phone}</DataTable.Cell>
<DataTable.Cell >{Donor.phone}</DataTable.Cell>
</DataTable.Row>
<DataTable.Pagination
page={1}
numberOfPages={3}
onPageChange={page => {
console.log(page);
}}
label="1-2 of 6"
/>
</DataTable>
<View>
<Text>List Data</Text>
<Text>{Donors.name}</Text>
</View>
</View>
)
})*/
}
</View>
);
}
export default Donorlist;
解决方案
一些原因
1)渲染项中的文本组件没有限制任何视图试试这个
<View> <Text>{item.name}</Text> </View>
keyExtractor={item=>item.key}
将此添加到 flatlist 中,其中键是字符串值,在您的数据中是唯一的,例如 id 或其他内容
这是我的代码,可能对你有帮助
const itembuilder=({item,index})=>
{
return(
<View
style={{
margin:10,borderRadius:20,shadowOpacity:0.5,
shadowOffset:{height:-15,width:15},
shadowColor:'#FFF8DC'
,justifyContent:'center',shadowRadius:25,elevation:15,
height:200,
marginBottom:20,
backgroundColor:'white'}}>
<Text>{item.name}</Text>
</View>
)
}
<FlatList
style={{height:500}}
data={product}
keyExtractor={item=>item.key}
renderItem={itembuilder}
>
</FlatList>
推荐阅读
- javascript - React native - 在渲染应用程序组件之前从异步存储中获取数据
- c# - 逗号作为C#中的数组长度是什么意思
- node.js - 模块导出:从 oracle 调用过程然后分配全局变量并在 Node js 中使用该变量的另一个文件?
- c# - 覆盖 GetRolesAsync
- c - .gitignore 除了 .c 和 .h 以外的所有东西递归
- python - 自动写入几个txt到目录
- r - 如何从 R 数据框中过滤今天的数据?
- javascript - 如何将数据从 javascript 函数发送到 Django View
- javascript - 使用 updateRowData 后 ag-Grid RowClassRule 未更新
- javascript - 从缓存加载页面时的JS或jQuery事件?