React API

Hooks exported by @omni-async/react.

useAction(handler, options?)

Creates an on-demand async action without retaining its resolved value.

Example:

const removeUser = useAction((id: string) => api.deleteUser(id));
await removeUser.trigger("42");

useAsync(handler, options)

Creates reactive state for an async handler with configurable concurrency and fallback data.

Example:

const save = useAsync((name: string) => api.saveProfile({ name }));
await save.trigger("Ada");

useFetch(handler, options?)

Runs an abortable fetch on mount and exposes controls for refetching or cancellation.

Example:

const profile = useFetch((signal) => fetchProfile({ signal }));
await profile.fetch();

useQuery(handler, options)

Creates a latest-request-wins query for data that may be refreshed on demand.

Example:

const users = useQuery(() => api.listUsers(), { initial: () => [] });
await users.trigger();

Shared result state

FieldTypeDescription
dataData | null | undefinedMost recently accepted data
errorunknown | nullMost recently accepted error
loadingbooleanWhether an accepted request is still running
trigger(...params: Params) => Promise<Data>Starts the handler with typed parameters

useAsync options

OptionDefaultDescription
initialDatanullInitial data and the value restored by the operation
concurrency"all"Use "latest" to prevent older requests updating state
dataOnErrorPreserveProduces replacement data after a rejected request
isEqualFieldsSuppresses updates when the previous and next states are equal
onSuccessRuns after an accepted successful update
onErrorRuns after an accepted error update

The operation is memoized from its structural options. Current handlers and callbacks are retained through refs, and active work is aborted when the component unmounts.

useQuery

useQuery uses "latest" concurrency. Its optional initial function supplies initial data and is called again to produce fallback data after an error.

useAction

useAction exposes error, loading, and trigger. The resolved value is returned by trigger instead of being retained as component state.

useFetch

useFetch calls fetch() on mount. Starting another fetch aborts the previous controller.

ControlDescription
fetch()Starts a request and returns its promise
abort()Aborts the current request, when one exists