React native Flatlist为空,数组中有值
我用React native Flatlist为空,数组中有值,react-native,google-cloud-firestore,React Native,Google Cloud Firestore,我用querySnapshot填充了一个数组,但flatlist没有呈现任何内容 试图更改renderItemcode constructor(props) { super(props); var rootref = firebase.firestore().collection("deneme"); var wholeData = [] rootref.get().then(function(querySnapshot){ querySnapshot.forEach
querySnapshot
填充了一个数组,但flatlist没有呈现任何内容
试图更改renderItem
code
constructor(props) {
super(props);
var rootref = firebase.firestore().collection("deneme");
var wholeData = []
rootref.get().then(function(querySnapshot){
querySnapshot.forEach(function(doc) {
wholeData.push(doc.data())
});
});
};
render() {
return (
<View>
<FlatList
data={this.wholeData}
renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
/>
</View>
);
};
构造函数(道具){
超级(道具);
var rootref=firebase.firestore().collection(“deneme”);
var wholeData=[]
rootref.get().then(函数(querySnapshot){
querySnapshot.forEach(函数(doc){
wholeData.push(doc.data())
});
});
};
render(){
返回(
{item.isim},{item.soyisim}
/>
);
};
您必须使用设置状态
通知组件数据已更改。更改代码以执行以下操作:
constructor(props) {
super(props);
this.state = {
data: []
};
var rootref = firebase.firestore().collection("deneme");
rootref.get().then(querySnapshot =>
const wholeData = querySnapshot.map(doc => doc.data()));
// notify your component that the data has changed
this.setState({
data: wholeData
})
};
render() {
return (
<View>
<FlatList
data={this.state.data} // get your data from the state
renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
/>
</View>
);
构造函数(道具){
超级(道具);
此.state={
数据:[]
};
var rootref=firebase.firestore().collection(“deneme”);
rootref.get().then(querySnapshot=>
const wholeData=querySnapshot.map(doc=>doc.data());
//通知组件数据已更改
这是我的国家({
数据:wholeData
})
};
render(){
返回(
{item.isim},{item.soyisim}
/>
);
这样,当您收到完整数据后,平面列表将立即更新。谢谢,但现在我遇到另一个错误querySnapshot不是函数。querySnapshot只是一个对象。如果错误地将其作为函数调用,则应检查代码。我复制了整个代码。常量部分给出了一个错误。