useInfiniteFetch
Loads a list page by page. Say which fields page through the request, and the hook takes care of the rest: every page is a normal request under its own cache key, and the hook glues them together.
A changed filter, sort or URL param starts a new list on its own. When to load more is up to you - call
fetchNextPage
from a button, an intersection observer,
onEndReached
or a virtualizer.
import { useInfiniteFetch } from "@hyper-fetch/react"
Usage
const { items, hasNextPage, fetchNextPage } = useInfiniteFetch(getFeed.setQueryParams({ topic }), {
pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
});
Parameters
useInfiniteFetch<R, P, TSelected>(request: R, options: UseInfiniteFetchOptionsType<R, P, TSelected>)
| Name | Type | Description |
|---|---|---|
| request | | Request instance |
| options | | Hook options,
|
Returns
type UseInfiniteFetchReturnType<R,P,TSelected> = {
abort: () => void;
bounce: { active: boolean; reset: () => void };
error: ExtractErrorType<R> | null;
extra: ExtractAdapterExtraType<ExtractAdapterType<R>> | null;
fetchNextPage: () => Promise<InfinitePageResponse<R> | null>;
fetchPreviousPage: () => Promise<InfinitePageResponse<R> | null>;
hasNextPage: boolean;
hasPreviousPage: boolean;
isFetchNextPageError: boolean;
isFetchPreviousPageError: boolean;
isFetching: boolean;
isFetchingNextPage: boolean;
isFetchingPreviousPage: boolean;
isPreviousData: boolean;
isRefetching: boolean;
items: TSelected;
loading: boolean;
onError: (callback: (event: InfinitePageEvent<R>) => void) => void;
onFinished: (callback: (event: InfinitePageEvent<R>) => void) => void;
onReset: (callback: () => void) => void;
onSuccess: (callback: (event: InfinitePageEvent<R>) => void) => void;
pageParams: unknown[];
pages: ExtractResponseType<R>[];
refetch: () => Promise<InfinitePageResponse<R> | null>;
reset: () => Promise<InfinitePageResponse<R> | null>;
status: ExtractAdapterStatusType<ExtractAdapterType<R>> | null;
success: boolean;
total: number | null;
}
| Name | Type | Description |
|---|---|---|
| bounce | | State of the bounce used for new lists |
| error | | Error of the last fetch |
| extra | | Additional data of the last response |
| hasNextPage | | Another page can be fetched after the loaded ones |
| hasPreviousPage | | Another page can be fetched before the loaded ones |
| isFetchNextPageError | | Last attempt of fetching the next page failed |
| isFetchPreviousPageError | | Last attempt of fetching the previous page failed |
| isFetching | | Any page of this list is being fetched |
| isFetchingNextPage | | |
| isFetchingPreviousPage | | |
| isPreviousData | | The list on screen belongs to the previous request, the new one is still loading |
| isRefetching | | Loaded pages are being fetched again |
| items | | Items of all the loaded pages |
| loading | | First page is loading and there is nothing to show yet |
| pageParams | | Paging values of the loaded pages |
| pages | | Response data of the loaded pages |
| status | | Status of the last response |
| success | | Last fetch succeeded |
| total | | Size of the whole list, when the pagination declares
|
| abort | | Cancels every page being fetched by this list |
| fetchNextPage | | Fetches the page after the loaded ones. Calls made while it loads share one request. |
| fetchPreviousPage | | Fetches the page before the loaded ones. Calls made while it loads share one request. |
| onError | | Called for every page that failed |
| onFinished | | Called for every finished page |
| onReset | | Called when the list starts over, for example to scroll back to the top |
| onSuccess | | Called for every page that succeeded |
| refetch | | Fetches all the loaded pages again and swaps the list when they arrive |
| reset | | Drops to the starting page and fetches it again |

