useAsyncListing
SourceSpecialised version of useAsync for retrieving a list of paginated values and optionally accumulating them as the next page is retrieved (e.g. for things like infinite scroll).
Unless you are using
paginator
withaccumulatePages
, or are writing a hook/component that may use those options based on its props, you don't need this hook and should just use useAsync.
Usage
While it can work with any async function the typical usage would be to use it with an Endpoint that has paginationMiddleware applied.
const list = new Endpoint("/list", { middleware: [paginationMiddleware()], }) function Example() { const paginator = usePaginator(list) const execute = useCallback(async (args) => { return (await list.execute(args)).result }, []) const { result, isLoading, error } = useAsyncListing({ execute, paginator, accumulatePages: true, }) if (isLoading) return <Loading /> if (error) return <Error error={error} /> return <List items={result} paginator={paginator} /> }
See the infinite scroll demo for a full working example.
Arguments:
Argument | Type | Description | |
---|---|---|---|
* | props An object with the properties below | ||
props.accumulatePages | boolean | Whether to accumulate pages as more results are fetched. For example if the first page of results is returned, then the next page is fetched then the combined results for the first two pages will be resolved. This resets whenever If this is true you must specify | |
* | props.execute | Asynchronous function that returns the result for the query. Passed an
object with Note that when | |
props.paginator | |null | Optional paginator if result is paginated. This will be monitored for
any state changes which will trigger a call to Required if | |
props.query | Record | Any query string parameters for the request | |
props.trigger | "MANUAL"|"DEEP" | When to trigger the fetch. Defaults to If set to Defaults to 'DEEP'. |
An object with these properties: | |||
---|---|---|---|
Property | Type | Description | |
* | error | null|Error | Set to the rejected value of the promise. Only one of Until first call has resolved neither error nor result will be set |
* | isLoading | boolean | True while |
* | paginator | null| | The same |
* | reset | When called will set both result and error to null. Will not immediately trigger
a call to the action but subsequent changes to query or paginator will according
to the value of | |
* | result | null|T | The value returned from execute. Only one of Until first call has resolved neither error nor result will be set |
* | run | A function to manually trigger the action. If This function will return a promise that resolves/rejects to same value
returned by |