Skip to main content
Version: v8.0.0

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
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>)
Parameters
NameTypeDescription
request
R

Request instance

options
UseInfiniteFetchOptionsType<R, P, TSelected>

Hook options, pagination is required

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;
}
useInfiniteFetch
NameTypeDescription
bounce
{ active: boolean; reset: () => void }

State of the bounce used for new lists

error
ExtractErrorType<R> | null

Error of the last fetch

extra
ExtractAdapterExtraType<ExtractAdapterType<R>> | null

Additional data of the last response

hasNextPage
boolean

Another page can be fetched after the loaded ones

hasPreviousPage
boolean

Another page can be fetched before the loaded ones

isFetchNextPageError
boolean

Last attempt of fetching the next page failed

isFetchPreviousPageError
boolean

Last attempt of fetching the previous page failed

isFetching
boolean

Any page of this list is being fetched

isFetchingNextPage
boolean
isFetchingPreviousPage
boolean
isPreviousData
boolean

The list on screen belongs to the previous request, the new one is still loading

isRefetching
boolean

Loaded pages are being fetched again

items
TSelected

Items of all the loaded pages

loading
boolean

First page is loading and there is nothing to show yet

pageParams
unknown[]

Paging values of the loaded pages

pages
ExtractResponseType<R>[]

Response data of the loaded pages

status
ExtractAdapterStatusType<ExtractAdapterType<R>> | null

Status of the last response

success
boolean

Last fetch succeeded

total
number | null

Size of the whole list, when the pagination declares total

abort
() => void

Cancels every page being fetched by this list

fetchNextPage
() => Promise<InfinitePageResponse<R> | null>

Fetches the page after the loaded ones. Calls made while it loads share one request.

fetchPreviousPage
() => Promise<InfinitePageResponse<R> | null>

Fetches the page before the loaded ones. Calls made while it loads share one request.

onError
(callback: (event: InfinitePageEvent<R>) => void) => void

Called for every page that failed

onFinished
(callback: (event: InfinitePageEvent<R>) => void) => void

Called for every finished page

onReset
(callback: () => void) => void

Called when the list starts over, for example to scroll back to the top

onSuccess
(callback: (event: InfinitePageEvent<R>) => void) => void

Called for every page that succeeded

refetch
() => Promise<InfinitePageResponse<R> | null>

Fetches all the loaded pages again and swaps the list when they arrive

reset
() => Promise<InfinitePageResponse<R> | null>

Drops to the starting page and fetches it again