liveBook · Manning

Handler

As explained in Web Component Development with Modern Libraries and Tooling

An event handler is a callback or method that executes in response to a specified event.

An event handler is a function or method that runs when an event occurs. A button-click handler can open a page; a change handler can react when a user selects a different radio button. Handlers let a component respond to its own controls, while listeners on the component let the surrounding app respond to events it dispatches. section 5.5 section 11.3

Handle a button click

In Lit, `<button @click=${this.handleClick}>Google It</button>` connects the click event to a method. That method calls `window.open(...)`. Stencil expresses the same connection as `onClick={this.handleClick}`; FAST uses `@click=${x => x.handleClick()}`. The syntax differs, but each connects a rendered button to code that runs when it is clicked. section 5.5 section 6.6 section 7.8

Handlers can also change component state. In a sortable table, each heading button calls `updateSort(property)`. That method updates the sort property and direction, then dispatches a `sort` event whose `detail` contains the new criteria. An app can register a listener on the `<sortable-table>` element to receive them. When an event must reach ancestors outside shadow DOM, dispatch it with `bubbles: true` and `composed: true`. section 5.9 section 4.2.14

Listing 4.16 Listening for "sort" events
const sortableTable = document.querySelector('sortable-table');
sortableTable?.addEventListener("sort", (event) => {
  const { property, descending } = event.detail;
  console.log(
    `The table is now sorted by ${property}`,
    `${descending ? "descending" : "ascending"}.`,
  );
});

See how an app attaches a `sort` listener to the component and reads the event’s `detail`. section 4.2.14

Attach a listener after its target exists. For example, when server-rendered radio-group HTML is inserted later, an insertion event handler finds the new component and then attaches its `change` listener. Inside the radio group, a separate handler listens for input changes and dispatches a change event for the host app. section 11.3

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Shows concrete event bindings, state updates, custom-event details, and listener placement without examining browser event internals.
Examples
Uses a button that opens a page, sortable column headings, radio-button changes, and a listener attached after HTML insertion.
What it assumes
Assumes familiarity with buttons and component markup while demonstrating event-binding syntax through small working components.
What you can do after
Apply it via Lit, Stencil, or FAST to handle clicks, and use DOM listeners to react to component events.
How it connects
Links event handlers to web components, DOM events, custom events, shadow DOM, component state, and server-side rendering.

Also explained in

Compare how 29 books explain handler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.