Skip to content

Function: useCommandHandler()

ts
function useCommandHandler<R, D>(type: CommandType, handler: CommandHandler<D, R>): void;

Defined in: src/wirestate-react/commands/use-command-handler.ts:31

Registers a command handler for the component's lifetime.

Type Parameters

Type ParameterDefault typeDescription
RunknownResult type of the command.
DunknownData/payload type of the command.

Parameters

ParameterTypeDescription
typeCommandTypeCommand type (string or symbol).
handlerCommandHandler<D, R>Command handler function.

Returns

void

Remarks

The handler is stored in a useRef and synced on every render to avoid stale closures without requiring manual memoization of the handler function. Only one handler is active per type; newer registrations shadow older ones. The handler is automatically unregistered when the component unmounts.

Example

tsx
useCommandHandler("SAVE_COMMAND", (data) => {
  return api.save(data);
});