Skip to main content

UseAsyncCollectionResT

import { type UseAsyncCollectionResT } from '@dr.pogodin/react-global-state';

UseAsyncCollectionResT is the type of result possibly returned by useAsyncCollection() hook (depends on hook's arguments).

It is defined as the generic type:

type UseAsyncCollectionResT<
DataT,
IdT extends number | string = number | string,
> = {
items: {
[id in IdT]: CollectionItemT<DataT>;
}
loading: boolean;
reload: AsyncCollectionReloaderT<DataT, IdT>;
timestamp: number;
};

// Where AsyncCollectionReloaderT and CollectionItemT are defined this way:

type CollectionItemT<DataT> = {
data: DataT | null;
loading: boolean;
timestamp: number;
};

type AsyncCollectionReloaderT<
DataT,
IdT extends number | string = number | string,
>
= (loader?: AsyncCollectionLoaderT<DataT, IdT>) => Promise<void>;

Generic Parameters

  • DataT — The type of data managed by useAsyncCollection() hook.
  • IdTnumber | string — The type of hook's id argument.

Fields

  • items{ [id in IdT]: CollectionItemT<DataT> } — The map of items requested from the hook. Similarly to UseAsyncDataResT, for each requested ID it will contain the DataT object or null, depending whether the global state has this object, fetched within the maxage limit; it will also contain the timestamp of last retrieval of this object, and loading flag indicating if a new retrieval is being in-flight.

    caution

    If data is null because async data loaded into the state do not satisfy the maxage limit, the value of timestamp still will correspond to the time when those async data were loaded into the state.

  • loadingboolean — This flag, at the root level of the response, is true if the loading operation is currently underway for any of the requested data object; or false otherwise (when all requested objects are within their refresh limits).

  • reload(loader?: AsyncCollectionLoaderT<DataT>) => Promise<void> — Imperatively triggers reloads of data for each of the requested IDs , using provided custom loader, if any, or otherwise the loader given to the corresponding useAsyncCollection() hook.

    Tips
    • This method is intended for imperative code (e.g. UI event handlers). When data should be reloaded in response to local or global state changes, prefer to use deps option of UseAsyncDataOptionsT to manage reloads.

    • This method is a stable function — it is guaranteed to remain the same across re-renders of the host component.

  • timestampnumber — The timestamp (milliseconds) at the root level of the result is the smallest of timestamp values for the requested objects.