React Infinite Query Pagination PageParams Issue #2135
-
Hi everyone I would like to ask regarding Typescript and Infinite Queries. At the moment I have a page variable in the state (using useState hook) which I pass down the components and I am having issues with it if I traverse back to the page. I can get all the data results as expected on first page: But if I traverse back, it returns me all the results which is good but it seems the pageParams is all wrong already (all the items are repeated as well 😭): My code inside the render function has this Infinite Query implementation:
I am using useState for my page variable because deep down in my components I need this information, hence I do the fetchNextPage() trigger via the useEffect() Am I using React Infinite Query wrongly? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
it would really help if you'd trim that example down to a bare minimum and make a runnable codesandbox example out of it. As it stands, I have no idea what this is doing or where the issue is 😅 |
Beta Was this translation helpful? Give feedback.
-
I tried creating a simple component that utilizes Infinite Query but it seems the getNextPageParam doesnt work, it keeps hitting the same page which just keeps duplicating the entries (Hope this example is more meaningful or clear 😅) : https://codesandbox.io/s/polished-frog-dq94u?file=/pages/index.tsx:0-1205
Page Params is duplicated based on React Query Devtools: |
Beta Was this translation helpful? Give feedback.
-
The result of from the example in the docs:
so you need to change your `fetchPeople function accoringly: - const fetchPeople = async (_, page = 1) => {
+ const fetchPeople = async ({ pageParam = 1 }) => { |
Beta Was this translation helpful? Give feedback.
The result of
getNextPageParam
will be injected into the queryFn as part of thequeryContext
, which is the first parameter to the queryFn. It's an object containing thequeryKey
as well as thepageParam
property, which is the value you seek.from the example in the docs:
so you need to change your `fetchPeople function accoringly: