Fix initial infinite swr request for pages > 1 (#367)
In case more than 1 page was requested for the initial request, there was a problem in case the first page already had no results. In that case the request for the second page always returned with the "isLoadMore" and "isLoading" flag set to true. Thus, incorrectly indicating that the request is still active. The "actual" request was already finished, but internally the "isLoadMore" flag was set incorrectly due to not considering if an actual request is active.
This commit is contained in:
@@ -66,8 +66,9 @@ export type AbortableSWRInfiniteResponse<Data = any, Error = any> = SWRInfiniteR
|
||||
|
||||
const isLoadingMore = (swrResult: SWRInfiniteResponse): boolean => {
|
||||
const isNextPageMissing = !!swrResult.data && typeof swrResult.data[swrResult.size - 1] === 'undefined';
|
||||
const isRequestActive = swrResult.isValidating;
|
||||
// SWR "isLoading" state is only updated for the first load, for every subsequent load it's "false"
|
||||
return !swrResult.isLoading && swrResult.size > 0 && isNextPageMissing;
|
||||
return !swrResult.isLoading && swrResult.size > 0 && isNextPageMissing && isRequestActive;
|
||||
};
|
||||
|
||||
const disableSwrInfiniteCache: Middleware = (useSWRNext) => (key, fetcher, config) => {
|
||||
|
||||
Reference in New Issue
Block a user