Function: useOnQuery()
ts
function useOnQuery<R, P, T>(type: T, handler: QueryHandler<R, P, T>): void;Defined in: src/wirestate-react/queries/use-on-query.ts:34
Registers a query handler for the component's lifetime.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Result type of the query. |
P | unknown | Payload type of the query. |
T extends QueryType | QueryType | Query type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Query type to handle. |
handler | QueryHandler<R, P, T> | Function that resolves the query 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 QueryBus to be bound in the active container or an ancestor.
Throws
WirestateError if the active container cannot resolve QueryBus.
Example
tsx
useOnQuery<{ id: string; value: string }, { id: string }>("GET_DATA", (payload) => {
return { id: payload.id, value: "Resolved" };
});