API: utilities
Async list
useAsyncList or injectAsyncList loads a list's pages through your loader, filtered by a text, aborting a stale load, for the virtual lists.
A list whose items come from a loader, page by page: useAsyncList (React, Vue) and
injectAsyncList (Angular) keep the items, the status and the error in the framework's state,
and give loadMore and setFilterText to the virtual lists
(ListboxVirtual, SelectVirtual,
ComboboxVirtual,
AutocompleteVirtual). Base UI leaves async loading to the
consumer; React Aria's useAsyncList was read for the behaviour. The items keep the loader's order.
It is a utility in its own entry, /async-list, which references no component, on the toolkit's
/async-list mechanism: you bring the fetching, it brings the order of the
pages.
Behaviour
- The loader gets
{ signal, filterText, cursor }and returns{ items, cursor? }:cursorisundefinedfor the first page, else the one the previous page returned; a page with no cursor (ornull) is the last (hasMorefalse). - The first page loads once mounted (Angular: after the first render). The first render, on a
server too, has
statusloadingand no items. loadMore()loads the next page and appends it; it does nothing while a page loads or when there is none. Wire it to a virtual list'sonLoadMore.setFilterText(text)loads the first page for the text;reload()the first page again. The items stay until the new page arrives.- A new load aborts the one in flight (its
signal), and a stale page is never used, whichever arrives first: typing1then12never shows the page of1. - An error sets
statustoerroranderrorto what the loader threw; the items stay. - Destroyed: the load in flight is aborted.
Usage
import { useAsyncList } from "@fragiola/primitives-react/async-list";
const list = useAsyncList<Item, number>({
load: async ({ signal, filterText, cursor = 0 }) => {
const response = await fetch(`/api/items?q=${encodeURIComponent(filterText)}&from=${cursor}`, { signal });
const page: { items: Item[]; next: number | null } = await response.json();
return { items: page.items, cursor: page.next };
},
});
<ListboxVirtual.Root aria-label="Items" items={list.items} loading={list.loading} onLoadMore={list.loadMore}>
{(item) => <Listbox.Item value={item.value}>{item.label}</Listbox.Item>}
</ListboxVirtual.Root>;Props
Options
| option | type | default | description |
|---|---|---|---|
load | ({ signal, filterText, cursor }) => Promise<{ items, cursor? }> | required: loads a page (AsyncListLoad) | |
filterText | string | "" | the first filter text |
Result
| name | type | description |
|---|---|---|
items | Item[] | the items of every page loaded |
filterText | string | the text the items were asked for |
status | "idle" | "loading" | "loading-more" | "error" | loading: the first page (or a new text); loading-more: the next one |
loading | boolean | status is loading or loading-more: a virtual list's loading |
error | unknown | what the last failed load threw, else undefined |
hasMore | boolean | the last page gave a cursor |
reload() | () => void | loads the first page again |
loadMore() | () => void | loads the next page: a virtual list's onLoadMore |
setFilterText(text) | (text: string) => void | loads the first page for a new text |
React returns them in its state (a new object on each change), Vue as a reactive object, Angular
as signals (items(), loading(), ...) and plain functions.
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/async-list | AsyncListLoad, AsyncListPage, AsyncListRequest, AsyncListResult, AsyncListState, AsyncListStatus, UseAsyncListOptions, useAsyncList |
@fragiola/primitives-vue/async-list | AsyncListLoad, AsyncListPage, AsyncListRequest, AsyncListResult, AsyncListState, AsyncListStatus, UseAsyncListOptions, useAsyncList |
@fragiola/primitives-angular/async-list | AsyncListLoad, AsyncListPage, AsyncListRef, AsyncListRequest, AsyncListStatus, InjectAsyncListOptions, injectAsyncList |