简体   繁体   English

如何乘以方法调用的数量来获取下一页数据(无限滚动分页)

[英]How to multiply the number of method calls to fetch next page data (infinite scrolling pagination)

I'm currently working on implementing an infinite scrolling pagination feature by applying the useState hook.我目前正在通过应用useState挂钩来实现无限滚动分页功能。 The fetchMoreListItems() function seen below uses every subsequent .follow('next') call to fetch the next page from the API.下面看到的fetchMoreListItems() function 使用每个后续.follow('next')调用从 API 获取下一页。 For example .follow('next').follow('next').followAll('item') will fetch all records on page 3.例如.follow('next').follow('next').followAll('item')将获取第 3 页上的所有记录。

This logic may sound silly, but I'm essentially trying to find an efficient way to "multiply" the amount of .follow('next') method calls times the page number set by setPage() to fetch that appropriate data to append to the DOM when the user scrolls to the bottom.这种逻辑可能听起来很愚蠢,但我实际上是在尝试找到一种有效的方法来“乘以” .follow('next')方法调用的数量乘以setPage()设置的页码以将适当的数据获取到 append 到用户滚动到底部时的 DOM。 For example if by infinite scrolling we currently see all data up until page 3, then I would need .follow('next') * 3 to fetch page 4 data when the user scrolls down.例如,如果通过无限滚动我们当前看到所有数据直到第 3 页,那么当用户向下滚动时,我需要.follow('next') * 3来获取第 4 页数据。 Is there a way to break up this series of method calls such that if I wanted page 4 data, then instead of calling .follow('next') 3 times, I could do something like this: .follow('activity-collection') +.follow('next') * 3 +.followAll('item') ?有没有办法分解这一系列方法调用,这样如果我想要第 4 页数据,那么我可以做这样的事情,而不是调用.follow('next') 3 次: .follow('activity-collection') +.follow('next') * 3 +.followAll('item')

const [items, setItems] = useState<Resource<Activity>[]>();
const [isFetching, setIsFetching] = useState(false);
const [page, setPage] = useState(1);

useEffect(() => {
    if (!props.resource) return;
    props.resource
      .follow('activity-collection').followAll('item')
      .then(resources => {
        setItems(resources);
        console.log(resources);
      })
      .catch(err => console.error('Error fetching resources: ' + err));
  }, [activityResource]);

function fetchMoreListItems() {
    setTimeout(() => {
      if (page >= 1) {
        props.resource
          // This line currently retrieves page 2 information 
          // every subsequent .follow('next') fetches the next page
          .follow('activity-collection').follow('next').followAll('item')
          .then(resources => {
            setItems([...items, ...resources]);
            console.log([...items, ...resources]);
            setPage(page + 1);
          })
          .catch(err => console.error('Error fetching resources: ' + err));
        setIsFetching(false);
        console.log(page);
      }
    }, 1000);
  }

This is doable using a bottom up recursive approach:这可以使用自下而上的递归方法来实现:


const [page, setPage] = useState(2);

const fetchFunction = (pageToLoad) => {

  // basecase
  if (pageToLoad === page) {
    return props.resource.follow('activity-collection');
  } else {
    return fetchFunction(pageToLoad + 1).follow('next')
  }
}

fetchFunction(0)

/// call chain for page = 2:

props.resource.follow('activity-collection').follow('next').follow('next')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM