Skip to content

Function: useOnCommand()

ts
function useOnCommand<R, P, T>(host: ReactiveElement, options: UseOnCommandOptions<R, P, T>): OnCommandController<R, P, T>;

Defined in: src/wirestate-lit/commands/use-on-command.ts:48

Registers a command handler for the host element's lifetime.

Type Parameters

Type ParameterDefault type
Runknown
Punknown
T extends CommandTypeCommandType

Parameters

ParameterTypeDescription
hostReactiveElementHost element.
optionsUseOnCommandOptions<R, P, T>Command handling options.

Returns

OnCommandController<R, P, T>

The command controller instance.

Remarks

Registers when the host connects, unregisters when it disconnects, and re-registers when the nearest container context changes. Command handlers are stack-based on the active command bus.

Example

typescript
class MyElement extends LitElement {
  private onSave = useOnCommand(this, {
    type: "SAVE",
    handler: (payload) => console.log("Saving:", payload),
  });
}