如何解决React Native Firebase查询快照
我想一次获取所有帖子,以限制对Firebase服务器的请求并提高速度。但是,我只希望FlatList
从状态中保存的所有数据中一次渲染20个帖子,以提高性能。基本上是从firebase获取所有帖子,但在设备上一次渲染20个帖子,但每20个帖子都不发出请求,然后当用户滚动到底部时,更多帖子从本地状态复制到本地状态,现在用户看到40个帖子。那可能吗。请帮忙
这是我获取所有帖子的方式,如何使逻辑通过FlatList限制本地发布:
const Posts = (props) => {
//getting all posts at once but want to limit them on device from two different states
// when user scrolls down the function adds 20 more posts
const [allPosts,setAllPosts] = useState();
const [loading,setLoading] = useState(true)
useEffect(() => {
getPosts();
},[]);
const getPosts = async () => {
try {
var all = [];
const unsubscribe = await firebase
.firestore()
.collection("Posts")
.orderBy("timestamp",'desc')
.get()
.then((querySnapshot) => {
querySnapshot.docs.forEach((doc) => {
all.push(doc.data());
});
setLoading(false);
});
setAllPosts(all);
if(currentUser === null){
unsubscribe()
}
} catch (err) {
setLoading(false);
}
};
}
解决方法
当用户到达列表的末尾时,可以使用FlatList组件中的onEndReached prop来调用函数。然后,您必须将您所在州的清单增加20。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。