Skip to content

Function: useOnCommand() ​

ts
function useOnCommand<R, P, T>(type: T, handler: CommandHandler<R, P, T>): void;

Defined in: src/wirestate-react/commands/use-on-command.ts:34

Registers a command handler for the component's lifetime.

Type Parameters ​

Type ParameterDefault typeDescription
RunknownResult type of the command.
PunknownPayload type of the command.
T extends CommandTypeCommandTypeCommand type.

Parameters ​

ParameterTypeDescription
typeTCommand type to handle.
handlerCommandHandler<R, P, T>Function that handles the command and returns its result.

Returns ​

void

Remarks ​

Only one handler is active per type. The newest registration shadows older ones. The handler is unregistered when the component unmounts or the active container changes, and may change between renders without re-registering. Requires CommandBus to be bound in the active container or an ancestor.

Throws ​

WirestateError if the active container cannot resolve CommandBus.

Example ​

tsx
const api = useInjection(ApiService);

useOnCommand("SAVE_COMMAND", (payload) => api.save(payload));