Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 39 ยท Events and Properties

Reflecting Attributes and Observed Changes

Keep attributes and properties in sync with observedAttributes, attributeChangedCallback and reflection, and avoid infinite loops and needless re-renders.

In the previous lesson you saw that attributes and properties are separate doors into a component. Real components usually need them to stay in sync. If someone writes el.open = true, CSS such as x-panel[open] { ... } should start matching. If someone edits the open attribute in DevTools, the component should react.

Two pieces make this work: observedAttributes with attributeChangedCallback (attribute to component), and reflection (property to attribute).

Observing attributes

A custom element only hears about attribute changes for names you list in a static observedAttributes getter. The browser then calls attributeChangedCallback(name, oldValue, newValue) whenever one of them is added, changed or removed, including once per attribute present in the initial markup.

attributeChangedCallback in action

Click the buttons and watch the log. When the attribute is removed, newValue is null, which is why the code falls back to "stranger".

Attributes that appear in the first markup trigger the callback before connectedCallback, and the element may not be rendered yet. Keep the callback light and let a shared render() method do the DOM work, as shown later in this lesson.

Reflecting properties back to attributes

Reflection means the property setter also updates the attribute. Built-in elements do this for many properties (input.type, a.href, button.disabled). The pattern for a boolean property looks like this:

JavaScript
get open() { return this.hasAttribute("open"); }
set open(value) {
  this.toggleAttribute("open", Boolean(value));
}

Here the attribute is the storage: the getter reads it, the setter writes it, and attributeChangedCallback performs the visible reaction. There is only one copy of the state, so sync problems cannot appear.

For a string or number, do the same with getAttribute / setAttribute and convert types in the getter.

A reflected open property

Because the CSS selector x-panel[open] matches in both cases, styling authors can hook into your component using ordinary attribute selectors. That is a major benefit of reflection.

Avoiding loops

If a setter writes the attribute and attributeChangedCallback writes the property, you can create an endless ping-pong. Two habits prevent it:

  1. Keep one source of truth (the attribute, or a private field), not both.
  2. Skip work when nothing changed: if (oldValue === newValue) return;.

Setting an attribute to the same value still fires the callback, so the equality check matters for expensive work.

Typed attribute helpers

When a component has many attributes, small helpers cut repetition. A numeric one:

JavaScript
get max() { return Number(this.getAttribute("max") ?? 100); }
set max(v) { this.setAttribute("max", String(v)); }

Remember that attribute names are case-insensitive in HTML, so choose lowercase names like max-items and map them to camelCase properties (maxItems) yourself.

Batching renders

Several attributes may change in the same tick (for example when a framework sets three at once). Rendering after each one wastes work. A microtask can collapse them:

Collapsing several changes into one render

All three changes land before the microtask runs, so the element renders once with consistent data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When is attributeChangedCallback called for a name?

    The browser ignores attributes that are not returned by the static observedAttributes list.
  2. 2. What is newValue when an observed attribute is removed?

    Removal is reported as null, which lets you tell it apart from an empty-string attribute.
  3. 3. What is the main advantage of using the attribute as the storage for a boolean property?

    The getter reads the attribute and the setter writes it, so CSS selectors, DevTools edits and scripts all see the same state.
  4. 4. Why queue renders in a microtask?

    All synchronous changes finish first, then one microtask renders consistent state.

Summary

  • List attribute names in static observedAttributes to receive attributeChangedCallback.
  • Reflect properties to attributes so CSS and DevTools stay in step.
  • Use one source of truth to avoid loops; compare old and new values.
  • Batch renders with queueMicrotask when several attributes change together.

Next: Custom Events and Communication