Skip to content

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 ParameterDefault typeDescription
RunknownResult type of the query.
PunknownPayload type of the query.
T extends QueryTypeQueryTypeQuery type.

Parameters

ParameterTypeDescription
typeTQuery type to handle.
handlerQueryHandler<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" };
});