[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/flow](../../README) / [index](../README) / on

# Function: on()

> **on**(`eventName`): (`fn`, `context`) => `void`

Defined in: [flow/src/decorators.ts:775](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/decorators.ts#L775)

Binds a method as a cross-component (realtime) event listener. Auto-exposes the method.

## Parameters

### eventName

`string` & `object`

The event to listen for; a known [FlowEvents](../interfaces/FlowEvents) key or any string.

## Returns

A method decorator that registers the listener.

(`fn`, `context`) => `void`

## Remarks

When the named event is dispatched (via `dispatch` / `dispatchTo` / `dispatchSelf`, or an
`echo:…` broadcast), the decorated method is invoked with the event payload. The event name
autocompletes to the known [FlowEvents](../interfaces/FlowEvents) contract while still accepting any string (for
`echo:…` broadcasts and gradually-typed events). Annotate the handler's parameter with
`EventPayload<"name">` to type the payload against the same contract. Because it auto-exposes,
no separate `@expose` is needed. Applies to a method.

## Example

```tsx
class Feed extends Component {
  @expose posts: Post[] = [];

  @on("post-created")
  onPostCreated(payload: EventPayload<"post-created">) {
    this.posts = [payload, ...this.posts];
  }
}
```
