[英]FlatList in React Native isn't rendering children from firebase
I am trying to add comments to a very basic social media app I am creating, by referencing the post that they want to comment to by key, and then having the user push their comments to the post as children. 我试图通过按键引用他们要评论的帖子,然后让用户将其评论作为孩子推送到帖子中,从而向正在创建的一个非常基本的社交媒体应用程序中添加评论。 Then, I will use a flatlist to render all of the comments. 然后,我将使用一个平面列表来呈现所有评论。
The FlatList is not rendering anything. FlatList不呈现任何内容。 I checked on firebase, and the comments are there, but when I try to run the flatlist, nothing renders. 我检查了Firebase,并且有注释,但是当我尝试运行Flatlist时,没有任何结果。 I would love some help getting the FlatList to render! 我希望获得一些帮助来呈现FlatList!
Getting the comments from firebase: 从firebase获取评论:
getItems(){
var items = [];
var query = ref.child(this.state.passKey).orderByKey();
query.once ('value', (snap) => {
snap.forEach ( (child) => {
items.push({
comment: child.val().comment,
});
});
}).then(() => {
this.setState({firebaseItems: items});
});
}
passKey being the key for the post as a string. passKey是该帖子的字符串键。 ref is just referencing the posts section in my firebase. ref只是引用我的Firebase中的posts部分。 Rendering the FlatList: 渲染FlatList:
<FlatList>
data = {this.state.firebaseItems}
renderItem={({ item, index }) =>
<View>
<View style={{width: parseInt(this.state.postWidth), height: ((item.content.length/3)*2 + 60), backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center', paddingLeft: 10, paddingRight:10, borderRadius:5}}>
<Text style={{fontSize: 18, color: '#000000', textAlign: 'center'}}>
{ item.comment }
</Text>
</View>
<View style={{width: 1, height: 4, backgroundColor: '#e8e8e8'}} />
</View>
}
</FlatList>
And the layout of my firebase: 和我的火力基地的布局:
posts:
-Kzrip74SH7430djfS:
content: 'This is a post. Above me is a random key example'
-KzGh589sjSJfjjds:
comment: 'this is a comment example. The key for the comment is nested at the same level as the content.'
-Kz5ghSr8uerSvjrnd:
comment: 'this is another comment.'
-Kzwehhherhwpgi:
content: 'this is another post.'
import React, { Component } from 'react';
import { FlatList, View, Text } from 'react-native';
import * as firebase from 'firebase/app';
class MessageList extends Component {
constructor(props) {
super(props);
this.state = { firebaseItems: {} };
}
componentWillMount() {
this.getItems();
}
getItems = () => {
var items = [];
firebase.database().ref('/posts').orderByKey().on('value', (snap) => {
snap.forEach((child) => {
items.push({
comment: child.val().comment,
});
});
this.setState({firebaseItems: items})
}, error => console.log(error));
};
renderRow = ({item, index}) => {
return (
<View style={{
width: 100,
height: ((item.length / 3) * 2 + 60),
backgroundColor: '#ffffff',
alignItems: 'center',
justifyContent: 'center',
paddingLeft: 10,
paddingRight: 10,
borderRadius: 5
}}>
<Text style={{
fontSize: 18,
color: '#000000',
textAlign: 'center'
}}>
{item.comment}
</Text>
</View>
);
};
render() {
return (
<FlatList data={this.state.firebaseItems}
renderItem={this.renderRow}/>
);
}
}
export default MessageList;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.