Skip to content

Function: useOnEvents()

Subscribes the component to events on the active container's EventBus.

Remarks

Pass a single event type, an array of types, or only a handler to receive every event, mirroring EventBus.subscribe. The subscription is scoped to the component and is removed automatically when it unmounts, the active container changes, or the set of listened types changes. Requires EventBus to be bound in the active container or an ancestor.

Param

typesOrHandler

The event type, an array of event types, or the handler itself to receive every event.

Param

maybeHandler

The handler invoked for matching events. Omit it when the first argument is the handler.

Throws

WirestateError if the active container cannot resolve EventBus.

Example

tsx
// Listen to one event type.
useOnEvents("CART_ITEM_ADDED", (event) => console.info(event.payload));

// Listen to several event types.
useOnEvents(["CART_ITEM_ADDED", "CART_VIEWED"], (event) => console.info(event.type));

// Listen to every event.
useOnEvents((event) => console.info(event.type));

Call Signature

ts
function useOnEvents(handler: EventHandler): void;

Defined in: src/wirestate-react/events/use-on-events.ts:16

Subscribes the component to every event on the active EventBus.

Parameters

ParameterTypeDescription
handlerEventHandlerHandler invoked for every event.

Returns

void

Call Signature

ts
function useOnEvents(type: EventType, handler: EventHandler): void;

Defined in: src/wirestate-react/events/use-on-events.ts:24

Subscribes the component to one event type on the active EventBus.

Parameters

ParameterTypeDescription
typeEventTypeEvent type to listen for.
handlerEventHandlerHandler invoked for matching events.

Returns

void

Call Signature

ts
function useOnEvents(types: readonly EventType[], handler: EventHandler): void;

Defined in: src/wirestate-react/events/use-on-events.ts:32

Subscribes the component to several event types on the active EventBus.

Parameters

ParameterTypeDescription
typesreadonly EventType[]Event types to listen for.
handlerEventHandlerHandler invoked for matching events.

Returns

void