我想一次获取所有帖子,以限制对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);
}
};
}