react-native - React Native Firestore 集合不呈现平面列表
问题描述
所以基本上我成功地从 Firestore 中检索了一个集合,并且成功地将它放在一个数组中,但它没有显示在屏幕上,问题是我会说我在 renderPost 函数中写参数帖子的位置。请帮助我,我猜这应该很容易,这是我的代码:
<!-- language-all: lang-js -->
import Fire, { getPosts } from "../Fire";
import firebase, { database } from "firebase";
require("firebase/firestore");
export default class HomeScreen extends React.PureComponent {
constructor(props) {
super(props);
}
state = {
loading: false,
limit: 2,
post: [],
user: {},
};
onPostsReceived = (post) => {
this.setState({ post: post });
console.log(this.state.post); //it DOES return an array full of objects of collection
};
componentDidMount() {
getPosts(this.onPostsReceived);
//console.log(this.state.post); //returns empty array
}
renderPost = (post) => {
return (
<View style={styles.feedItem}>
<Image
source={
this.state.user.avatar
? { uri: this.state.user.avatar }
: require("../assets/tempAvatar.jpg")
}
style={styles.avatar}
/>
<View style={{ flex: 1 }}>
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
}}
>
<View>
{/* <Text>{console.log(this.state.post)}</Text> */}
<Text style={styles.name}>{}</Text>
<Text style={styles.timestamp}>
{moment(post.timestamp).toDate().toDateString()}
</Text>
</View>
<Ionicons name="ios-more" size={24} color="#73788B" />
</View>
<Text style={styles.post}>{post.text}</Text>
<Image
source={post.image && { uri: post.image }}
style={styles.postImage}
resizeMode="cover"
/>
<View style={{ flexDirection: "row" }}>
<Ionicons
name="ios-heart-empty"
size={24}
color="#73788B"
style={{ marginRight: 16 }}
/>
<Ionicons name="ios-chatboxes" size={24} color="#73788B" />
</View>
</View>
</View>
);
};
render() {
return (
<View style={styles.container}>
<StatusBar
translucent
backgroundColor="white"
barStyle="dark-content"
/>
<ClassicHeader
headerTitle="Eleph"
leftComponent={
<TouchableOpacity
style={{ marginRight: 0, margin: 10 }}
onPress={() =>
this.props.navigation.dispatch(DrawerActions.openDrawer())
}
>
<FontAwesome5 name="bars" size={24} color="#161924" />
</TouchableOpacity>
}
rightComponent={
<TouchableOpacity
style={{ marginLeft: 0, margin: 10 }}
onPress={() => this.props.navigation.navigate("Message")}
>
<Ionicons name="ios-chatboxes" size={24} color="#73788B" />
</TouchableOpacity>
}
/>
<FlatList
style={styles.feed}
data={this.state.post}
//extraData={this.state}
renderItem={({ item, index }) => {
this.renderPost(item);
}}
keyExtractor={(item, index) => String(index)}
//ItemSeparatorComponent={this.renderSeparator}
ListFooterComponent={this.renderFooter}
ListHeaderComponent={this.renderHeader}
onEndReachedThreshold={0}
onEndReached={this.retrieveMore}
showsVerticalScrollIndicator={false}
></FlatList>
</View>
);
}
}
如果我的方法 getPosts from Fire 在这里很重要,那就去:
导出异步函数 getPosts (PostsRetrived) { var post = [];
var snapshot = await firebase.firestore()
.collection('posts')
.orderBy('timestamp')
.get()
snapshot.forEach((doc) => {
const PostItem = doc.data();
PostItem.id = doc.id;
post.push(PostItem);
});
PostsRetreived(post);
}
Fire.shared = new Fire(); 导出默认火;
解决方案
Omfg ...这就是我经常讨厌编程的原因。我为此损失了一个星期,错误是这样的:
renderItem={({ item, index }) => {
this.renderPost(item);
}}
should be renderItem={({ item, index }) =>
this.renderPost(item);
}
是的,我只需要删除一个花括号,没有任何地方指出这是有问题的 omg。无论如何,是的,再见
推荐阅读
- c++ - 覆盖模板结构的 << 运算符
- c# - 在 winforms 中显示始终处于活动状态的表单
- python - 如何在 Python Pandas 的 DataFrame 中根据日期创建列?
- java - 为什么当我对包装类做同样的事情时,java浮点文字不能提升为双精度?
- xor - (A XOR B) 与 (!A XOR !B) 不一样吗?
- javascript - 粘性标题导航菜单,当我向下滚动页面时,顶部有间隙
- azure-devops - 在 Azure DevOps 中防止压缩 VSBuild 任务
- scala - 为什么我不能使用 scala 解析器组合器解析我的代码
- neo4j - 查询以在没有克隆过程的情况下在 neo4j 中复制具有关系的节点
- java - java.lang.IllegalStateException:此错误处理程序无法处理'org.apache.kafka.common.errors.SslAuthenticationException's;