2022-04-11
1892
#react
Chiamaka Umeh
103049
Apr 11, 2022 ⋅ 6 min read

Pagination and infinite scroll with React Query v3

Chiamaka Umeh A frontend developer with a passion for designing highly-responsive user interfaces for JavaScript-based web and mobile apps using React and React Native.

Recent posts:

A Comprehensive Guide To JavaScript Generators

A comprehensive guide to JavaScript generators

JavaScript generators offer a powerful and often overlooked way to handle asynchronous operations, manage state, and process data streams.

Fimber Elemuwa
Jan 24, 2025 ⋅ 8 min read
​​Solving Micro-Frontend Challenges With Module Federation

​​Solving micro-frontend challenges with Module Federation

webpack’s Module Federation allows you to easily share code and dependencies between applications, helpful in micro-frontend architecture.

Peter Aideloje
Jan 23, 2025 ⋅ 7 min read
typescript object destructuring

TypeScript object destructuring and you

Whether you’re part of the typed club or not, one function within TypeScript that can make life a lot easier is object destructuring.

Lewis Cianci
Jan 22, 2025 ⋅ 5 min read
Using Firebase For ASP.NET Authentication

Using Firebase for ASP.NET authentication

Firebase is one of the most popular authentication providers available today. Meanwhile, .NET stands out as a good choice for […]

Lewis Cianci
Jan 21, 2025 ⋅ 9 min read
View all posts

6 Replies to "Pagination and infinite scroll with React Query v3"

  1. Hello, well i would like to point a potential problem for this code.
    “`js
    const {
    isLoading,
    isError,
    error,
    data,
    fetchNextPage,
    isFetching,
    isFetchingNextPage
    } = useInfiniteQuery([‘colors’], fetchUsers, {
    getNextPageParam: (lastPage, pages) => {
    return lastPage.info.page + 1
    }
    })
    “`
    When you get the lastPage.info.page + 1 the react-query will never understand when the pages has come to an end, so to fix this you need to put a nextPage that says if theres a next page like has been done on react-query documentation https://github.com/TanStack/query/blob/main/examples/load-more-infinite-scroll/pages/index.js

  2. Hi,

    FYI – looking at your code you’re calling the fetchNextPage like this:

    Load More

    However based on the documentation, this should be avoided instead the callback should be passed like this:

    fetchNextPage()}>Load More

    You can read about it here: https://tanstack.com/query/v4/docs/guides/infinite-queries?from=reactQueryV3&original=https://react-query-v3.tanstack.com/guides/infinite-queries

    Quote from the docs:
    “Note: It’s very important you do not call fetchNextPage with arguments unless you want them to override the pageParam data returned from the getNextPageParam function. e.g. Do not do this: as this would send the onClick event to the fetchNextPage function.”

Leave a Reply