React Native Firebase查询快照

时间:2020-09-07 09:53:07

标签: javascript firebase react-native react-native-flatlist 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);
    }
  };
}

1 个答案:

答案 0 :(得分:0)

当用户到达列表的末尾时,可以使用FlatList组件中的onEndReached prop来调用函数。然后,您必须将您所在州的清单增加20。