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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Result type of the command. |
D | unknown | Data/payload type of the command. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command type (string or symbol). |
handler | CommandHandler<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);
});