Skip to content

hooks/usePendingAction#

usePendingAction()#

function usePendingAction<A>(action, options?): PendingAction<A>;

Busy state for a user action: disabled while pending, one run at a time, and an inline progress flag once the action has taken long enough to need one.

Args: action: The work to do. May be sync or async; its result is ignored. options: Error reporting and the progress delay.

Returns: The pending flags and a guarded run to wire to the control.

Type Parameters#

Type Parameter
A extends unknown[]

Parameters#

Parameter Type
action (...args) => unknown
options? UsePendingActionOptions

Returns#

PendingAction<A>


PENDING_PROGRESS_DELAY_MS#

const PENDING_PROGRESS_DELAY_MS: 150 = 150;

How long an action runs before its inline progress indicator appears. Shorter than this, the pressed and disabled states are acknowledgement enough and a spinner would only flicker.


PendingAction#

Type Parameters#

Type Parameter
A extends unknown[]

Properties#

pending#

pending: boolean;

True from the moment the action starts until it settles. Disable the control on it.

run#

run: (...args) => Promise<void>;

Starts the action, unless one is already running, in which case the call is dropped. Never rejects: a thrown error is reported and clears the busy state.

Parameters#
Parameter Type
...args A
Returns#

Promise<void>

showProgress#

showProgress: boolean;

True once the action has run longer than the progress delay. Show the inline indicator on it.


UsePendingActionOptions#

Properties#

errorMessage?#

optional errorMessage?: string;

What the user was trying to do, as a sentence in their language. Shown if the action throws. Ignored when onError is given.

onError?#

optional onError?: (error) => void;

Replaces the default error notification.

Parameters#
Parameter Type
error unknown
Returns#

void

progressDelayMs?#

optional progressDelayMs?: number;

Delay before showProgress turns on. Defaults to PENDING_PROGRESS_DELAY_MS.