Skip to content

Commit

Permalink
docs(examples/react): fix prefetch pagination logic when trying to qu…
Browse files Browse the repository at this point in the history
…ickly get to the last page (#4564)

* bugfix/pagination-example-fetch-next-page: fix basing decision whether next page should be refetched on information from two pages prior

 In the useQuery call we set "keepPreviousData", so we must make sure that for fetching the next page we don't base our logic on the data from potentially two pages away.
If user would click very fast to get to the last page we would potentially load eleventh page and that could result in an error depending on the backend implementation.

* bugfix/pagination-example-fetch-next-page: update useEffect dependency array to reflect all used variables
  • Loading branch information
wojciechSlodziak committed Nov 30, 2022
1 parent 16000b8 commit 23bd564
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/react/pagination/pages/index.js
Expand Up @@ -35,12 +35,12 @@ function Example() {

// Prefetch the next page!
React.useEffect(() => {
if (data?.hasMore) {
if (!isPreviousData && data?.hasMore) {
queryClient.prefetchQuery(['projects', page + 1], () =>
fetchProjects(page + 1),
)
}
}, [data, page, queryClient])
}, [data, isPreviousData, page, queryClient])

return (
<div>
Expand Down

0 comments on commit 23bd564

Please sign in to comment.