Skip to content
Discussion options

You must be logged in to vote

I think it's a closure problem. When you call fetchNextPage, react-query calls the queryFn, which closures over page from the local state from the time it was created, so it doesn't know about the increment (setPage(page +1 )) that you have just done.

I don't think you need the local state for this. react-query injects the result from getNextPageParam into the queryFn as pageParam (have a look at the examples). So you will get whatever getNextPageParam produces:

- const [page, setPage] = useState(1);
const perPage = 10;
const { data, fetchNextPage, hasNextPage } = useInfiniteQuery(
    ['data', page, perPage],
-    () =>
+    ({ pageParam = 1 }) =>
      getPaginatedProductItems({
       …

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@joeynimu
Comment options

Comment options

You must be logged in to vote
2 replies
@joeynimu
Comment options

@TkDodo
Comment options

Answer selected by joeynimu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants