Created
June 30, 2020 18:59
-
-
Save anthowen/7cf9a9dfe75e698d37b61282b73bfb8b to your computer and use it in GitHub Desktop.
Pagination implementation using react-query. Thanks @tannerlinsley for this awesome library!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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