Skip to content

fetch new data in hasMore #9

Answered by buzinas
ALi-Hosseinzad asked this question in Q&A
Discussion options

You must be logged in to vote

You need to add the API call as a side effect that depends on page returned by the hook. And you also need a component that renders in case hasMore is true. Look at the example below:

import React from 'react';
import useInfiniteScroll from '@closeio/use-infinite-scroll';

export default function MyComponent() {
  const [items, setItems] = useState([]);
  const [hasMore, setHasMore] = useState(false);
  const [page, loaderRef, scrollerRef] = useInfiniteScroll({ hasMore });

  // This effect runs every time `page` changes
  useEffect(async () => {
    const data = await myApiCall({ page });
    setHasMore(data.hasMore);
    setItems(prev => [...prev, data.items]);
  }, [page]);

  return (

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by buzinas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #9 on December 15, 2020 21:54.