如何使用响应数据作为使用的选项
在我想使用useInfinitequery的页面中,
const { data, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage, status } = useInfiniteQuery(
['posts', searchState],
({ pageParam = 1 }) => post.search({ ...searchState, pageParam }),
{
getNextPageParam: (lastPage, pages) => {
if (pages.length < 2) return pages.length + 1;
else return undefined;
},
},
);
这是我的自定义axios func:
export const post = {
search: async (params: { keyword: string; sort: string; region: string; pageParam: number }) => {
const { keyword, sort, region, pageParam } = params;
const sortValue = SortType[sort as keyof typeof SortType];
const url = `/post/search?keyword=${keyword}&page=${pageParam}&filter=${sortValue}®ion=${region}`;
console.log(url);
const res = await client.get(url);
return res;
},
}
res in post.search函数返回:
{"status":200,"message":"search success","data":{"posts":[],"pageCount":0}}
我想将pagecount用作getNextpageParam func,hasnextpage value的条件。
我真正想问的是,如何在以下代码中使用PageCount值:
getNextPageParam: (lastPage, pages) => {
In the page where i wanna use useInfiniteQuery
const { data, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage, status } = useInfiniteQuery(
['posts', searchState],
({ pageParam = 1 }) => post.search({ ...searchState, pageParam }),
{
getNextPageParam: (lastPage, pages) => {
if (pages.length < 2) return pages.length + 1;
else return undefined;
},
},
);
This is my custom axios func:
export const post = {
search: async (params: { keyword: string; sort: string; region: string; pageParam: number }) => {
const { keyword, sort, region, pageParam } = params;
const sortValue = SortType[sort as keyof typeof SortType];
const url = `/post/search?keyword=${keyword}&page=${pageParam}&filter=${sortValue}®ion=${region}`;
console.log(url);
const res = await client.get(url);
return res;
},
}
res in post.search function returns:
{"status":200,"message":"search success","data":{"posts":[],"pageCount":0}}
And I'd like to use pageCount as a condition for the getNextPageParam func, hasNextPage value.
What i really want to ask is, how can i use pageCount value in the following code:
getNextPageParam: (lastPage, pages) => {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何不习惯使用React-Query的人。
您只需使用LASTPAGE查找res.data
For anyone who is not used to using react-query.
You can just simply look up the res.data using lastPage