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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Result type of the command. |
P | unknown | Payload type of the command. |
T extends CommandType | CommandType | Command type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Command type to handle. |
handler | CommandHandler<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));