【发布时间】:2022-10-15 15:57:13
【问题描述】:
我的无限查询钩子看起来像这样
const {
data,
error,
isSuccess,
hasNextPage,
fetchNextPage,
isFetchingNextPage
}=useInfiniteQuery(
'test',
({ pageParam = 1 }) => fetchData(pageParam, 14),
{
getNextPageParam: (lastPage: any, allPages: any) => {
console.log('endCursor', lastPage.nextCursor);
if (hasNextPage) {
console.log('Returning from keep fetching', lastPage.nextCursor);
return lastPage.nextCursor;
}
return undefined;
}
}
);
我的 fetch 函数看起来像这样
const fetchData = async (page = 1, pageSize: any, variables?: string) => {
console.log('Inside fetch data for new schema');
const res = await graphQLClient.request(
QUERY,
variables,
requestHeaders
);
console.log('res', res);
return {
pages: res.getCustomerLibrary.books.edges,
hasNextPage: res.data.books.pageInfo.hasNextPage,
lastPage: { nextCursor: res.data.books.pageInfo.endCursor },
nextPage: res.data
};
};
数据正常,但我的问题是如何更新 lastPage.nextCursor 以便我可以在 useInfiniteQuery 挂钩中使用它。
现在它对所有页面都未定义。
另外,我是不是做错了什么,因为数据没有正确返回,无论我从 fetchData 函数返回什么,一切都在数据的 pages 变量中,我想要的是:
- 从 fetch 函数返回的 pages 变量应该转到 pages
- 从 fetch 函数返回的hasNextPage 应该填充infiniteQuery 的hasNextPage 挂钩
- lastPage.nextCursor 应该填充到 pageParam 中,以便我可以使用它来获取下一页
【问题讨论】:
标签: reactjs react-query