Skip to content

Function: useEvent()

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

Defined in: src/wirestate-react/events/use-event.ts:27

Subscribes a component to a specific event type on the EventBus.

Parameters

ParameterTypeDescription
typeEventTypeEvent type to listen for.
handlerEventHandlerFunction invoked when the specified event is emitted.

Returns

void

Remarks

The subscription is active for the component's lifetime and is automatically cleaned up on unmount. The handler is synced via useRef to avoid stale closures without requiring manual memoization of the handler function.

Example

tsx
useEvent("USER_LOGGED_IN", (event) => {
  console.log("User logged in:", event);
});