gpt4 book ai didi

javascript - React Query v3 useInfiniteQuery 返回 isLoading,isFetching 始终为真,isFetchingNextPage 始终为假

转载 作者:行者123 更新时间:2023-12-04 03:45:01 25 4
gpt4 key购买 nike

我已经使用 react-query v3 中的 useInfiniteQuery 实现了无限滚动。但 isLoading, isFetching 在第一页查询后始终为真。并且 isFetchingNextPage 始终为 false。初始加载第一页后,使用 fetchNextPage()

发出下一页请求

这是我的useContents钩子(Hook)。

const useContents = (params) => {
return useInfiniteQuery(
['contents', params.Section],
({ pageParam = 0 }) => fetchContents(pageParam, params), {
getNextPageParam: (lastPage, allPages) => {
if (lastPage.payload.size === PAGE_SIZE) return allPages.length;
return false;
},
refetchInterval: 60000,
staleTime: 60000,
}
);
};

这是fetchContents

const fetchContents = (pageParam, params) => {
return axios
.get(`${apiDomain}/contents`, {
params: {
Section: params.Section,
ViewType: params.ViewType,
Count: pageParam*PAGE_SIZE,
PageSize: PAGE_SIZE,
...params.questionSectionParam,
},
...generateAxiosOptions('GET'),
})
.then((res) => {
return {
message: res.data.message,
payload: fromJS(res.data.payload),
result: res.data.result,
status: res.status,
pageParam,
};
});
};

我已经花了几个小时但找不到原因。

最佳答案

事实证明这是一个愚蠢的错误。我向来自具有空数组的 Prop 的 ref 添加了一个滚动事件监听器。在错误代码下方并通过删除空数组进行修复。

useEffect(() => {
!!contentListRef && contentListRef.addEventListener('scroll', infiniteScroller, false);
return () => {
lastScrollTop = 0;
contentListRef.removeEventListener('scroll', infiniteScroller, false);
};
}, []); // <-- This empty array is the reason for this bug

关于javascript - React Query v3 useInfiniteQuery 返回 isLoading,isFetching 始终为真,isFetchingNextPage 始终为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65322309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com