> For the complete documentation index, see [llms.txt](https://atomico.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://atomico.gitbook.io/doc/api/hooks/useevent.md).

# useEvent

## Syntax

```javascript
const dispatchEvent = useEvent(myEvent, eventInit);
```

Where:

* dispatchEvent: **callback,** dispatches the event from the webcomponent and allows defining the detail by receiving a first parameter
* myEvent: **string**, name of the event to dispatch.
* eventInit: **optional object**, event configuration.

## Examples

{% tabs %}
{% tab title="Basic" %}

```jsx
import { useEvent } from "atomico";

function component() {
  const dispatchEvent = useEvent("clickButton", {
    bubbles: true,
    composed: true,
  });
  return (
    <host>
      <button onclick={() => dispatchEvent()}>button</button>
    </host>
  );
}
```

{% endtab %}

{% tab title="Detail" %}

```javascript
import { useEvent } from "atomico";

function component() {
  const dispatchEvent = useEvent("clickButton", {
    bubbles: true,
    composed: true,
  });
  return (
    <host>
      <button onclick={() => {
        const detail = "my-component"; // 👈
        dispatchEvent(detail);         // 👈
      }}>button</button>
    </host>
  )c
```

{% endtab %}

{% tab title="Typescript ⚡ " %}

```typescript
import { Host, useEvent } from "atomico";

type DetailClickButton = {id: number};
//                             👇 declaration to associate event to JSX/TSX
function component():Host<{onclickButton: CustomEvent<DetailClickButton>}> {
//                             👇 type for detail
  const dispatchEvent = useEvent<DetailClickButton >("clickButton", {
    bubbles: true,
    composed: true,
  });
  return (
    <host>
      <button onclick={() => {
        //            👇 Detail
        dispatchEvent({id:100});
      }}>button</button>
    </host>
  );
}
```

{% endtab %}
{% endtabs %}

## Event customization

The second parameter of `useEvent` allows customizing the behavior of the even:

```typescript
interface EventInit {
  // Allows the event to be dispatched upstream to the node's containers.
  bubbles?: boolean;
  // Allows the event to traverse the shadowDOM event capture.
  composed?: boolean;
  // Allows the event to be canceled.
  cancelable?: boolean;
  // Allows customizing the event builder, ideal for event instance-based communication.
  base?: Event | CustomEvent;
}
```

## Recommended articles

{% content-ref url="/pages/xh5sNrmkFJoi8kmgMTsU" %}
[How to declare events for your component at the type level for TSX?](/doc/guides/frequent-questions/how-to-declare-events-for-your-component-at-the-type-level-for-tsx.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://atomico.gitbook.io/doc/api/hooks/useevent.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
