Skip to content

Instantly share code, notes, and snippets.

@anthowen
Created June 30, 2020 18:59
Show Gist options
  • Save anthowen/7cf9a9dfe75e698d37b61282b73bfb8b to your computer and use it in GitHub Desktop.
Save anthowen/7cf9a9dfe75e698d37b61282b73bfb8b to your computer and use it in GitHub Desktop.
Pagination implementation using react-query. Thanks @tannerlinsley for this awesome library!
let [hasNextPage, setHasNextPage] = React.useState(true)
const { data } = useInfiniteQuery(key, (key, nextPage) => {
const data = fetch('/api?page=' + nextPage)
if (data.length) {
return data
} else {
setHasNextPage(false)
return []
}
}, {
getFetchMore: (lastPage) => hasNextPage
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment