liveBook · Manning

Observedattributes

As explained in Web Component Development with Modern Libraries and Tooling

A static custom-element class member that specifies the attribute names whose changes invoke attributeChangedCallback.

A custom element uses the static observedAttributes getter to name the HTML attributes it watches. When one of those attributes changes, attributeChangedCallback receives its name and new value. Listing an attribute is only the first step: the callback must decide how to update the component. section 3.10

A radio group that responds to markup

A <radio-group> can accept labels, legend, name, value, and values as attributes. Its class returns those five names from static get observedAttributes(). In attributeChangedCallback, it assigns the new value to the matching property. Once connected, changes other than value trigger render(); changing value instead uses the value setter to update the selected radio button without replacing the whole DOM. section 3.10

Initial attributes also need care. The radio group waits until connected before re-rendering, avoiding a render for each initial attribute value. Its connectedCallback reads the supported attributes and performs the initial render. section 3.10

Keep attributes and properties in sync

A <sortable-table> observes string-valued headings and properties, but not data, which holds an array of objects. Its attributeChangedCallback routes attribute changes through the corresponding property setters. A setter can update the attribute and the displayed table; checking whether the value actually changed prevents that synchronization from recurring indefinitely. section 4.2.8

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Shows a five-name static getter and follows changes through a callback, initial rendering, and setters; does not examine browser internals.
Examples
Uses a radio-group component with five observed attributes and a sortable-table component that observes headings and properties but not array-valued data.
What it assumes
Assumes JavaScript class and HTML attribute basics while showing the custom-element getter and callback wiring directly.
What you can do after
Build it from scratch: choose observed attributes, route their changes through a callback, and avoid unnecessary renders or recursive updates.
How it connects
Links attributeChangedCallback with connectedCallback, property setters, rendering, and attribute–property synchronization.

Also explained in

Compare how 2 books explain observedattributes →

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.