javascript - 使用 react-infinite-scroll-component 的 Firestore 限制仅在给定限制下滚动

标签 javascript reactjs google-cloud-firestore

我正在尝试使用 react-infinite-scroll-component 使用我的 firestore 文档列表创建无限滚动。我将限制设置为 5 以进行测试。无限滚动正在工作,但它只显示 5 个列表的给定限制:

目前我创建了 25 个不同的列表,我将 firestore 限制为一次显示 5 个:

list 1
list 2
list 3
list 4
list 5
loading more...

加载后它将加载相同的 5 个列表并且不加载新列表,并且还会在给定的 5 个限制下无限循环加载:

list 1
list 2
list 3
list 4
list 5
list 1
list 2
list 3
list 4
list 5
loading more...

下面是我获取查询的示例代码,我将其放在 useEffect 中,因为它位于用户页面和主页中的上下文提供程序包装中,两者都使用无限滚动:

 import InfiniteScroll from 'react-infinite-scroll-component';

 const [list, setList] = useState([]);
 const [last, setLast] = useState(null);


 useEffect(() => {

   firestoreList
    .orderBy('createdAt', "desc")
    .limit(5)
    .get()
    .then((querySnapshot) => {
       const lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1];
       setLast(lastVisible.data());

       const postList = []
       querySnapshot.forEach((doc) => {
         postList.push(doc.data()); 
       })
       setList(postList);
    })
    .catch((err) => {
       console.log(err)
    })
  },[]};

这是获取数据的代码:

 const {list, last, setList } = ListContext(); 

 const fetchMoreData = () => {
    firestoreList
      .orderBy('createdAt', "desc")
      .startAfter(last)
      .limit(5)
      .get()
      .then(() => {
         setList(list.concat(list));
      })
      .catch((err) => {
         console.log(err)
      })
 };

对于 react-infinite-scroll-component:

   <InfiniteScroll
     dataLength={list.length}
     next={fetchMoreData}
     hasMore={true}
    >
       {list.map((a, i) => (
         <div key={i}>
           {a.allList}
         <div>
       ))}
   <InfiniteScroll>

如何正确设置限制并使其他列表出现并停止循环?

最佳答案

我现在终于让它工作了,我有了关于这个的想法 youtube channel ,我也 应该在我的 fetchMoreData 中定义了一个 lastVisible,并停止给出未定义错误的循环。

const {list, last, setList, setLast } = ListContext(); 

const [notify, setNotify] = useState(null);

const fetchMoreData = () => {
  const field = "createdAt";

  firestoreList
    .orderBy(field, "desc")
    .startAfter(last[field]) //from Fireship channel, which loads the next data
    .limit(5)
    .get()
    .then((querySnapshot) => {
       const lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1];

       const postList = []
       querySnapshot.forEach((doc) => {
         postList.push(doc.data());
       })

       if (lastVisible !== undefined) {
         setList([...list, ...postList]);
         setLast(lastVisible.data());

       } else {
         setNotify("nothing to load.");
         return;
       }

    })
    .catch((err) => {
      console.log(err)
    })
};

即使不使用“react-infinite-scroll-component”,它也可以工作,只需使用 useEffect。

关于javascript - 使用 react-infinite-scroll-component 的 Firestore 限制仅在给定限制下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64169968/

相关文章:

javascript - 更新 JavaScript 函数中的进度指示器

javascript - 如何访问 JSON 文件的一部分

reactjs - 如何扩展 JSX.IntrinsicElements ['div' ]? TS2499

reactjs - 如何在应用程序的客户端内使用 router.replace?

css - 在 reactjs 中悬停显示组件

javascript - 在django中长时间处理时显示加载gif?

javascript - 如何 stub 未直接传递给调用函数的函数?

javascript - 火存储 : storing data to fields with parameter names

android - 我应该从 Android 客户端还是从 Firebase Function 保存到 Firestore?

firebase - 由于达到 Firestore 限制而进行扩展时