For the complete documentation index, see llms.txt. This page is also available as Markdown.

useEvent

Dispatch events from the webcomponent without referencing the context(this)

Syntax

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

import { useEvent } from "atomico";

function component() {
  const dispatchEvent = useEvent("clickButton", {
    bubbles: true,
    composed: true,
  });
  return (
    <host>
      <button onclick={() => dispatchEvent()}>button</button>
    </host>
  );
}
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

Event customization

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

How to declare events for your component at the type level for TSX?

Last updated

Was this helpful?