General-purpose base for writing components in functional-reactive style


ReactiveElement is a general-purpose base for writing components in functional-reactive style. It simply bundles a small set of mixins:

  1. AttributeMarshallingMixin, which converts changes to element attributes into changes in element properties.
  2. ReactiveMixin, which gives a component class a functional-reactive programming (FRP) architecture that can track internal state and render that state to the DOM.
  3. ShadowTemplateMixin, which handles the task of populating the component's shadow root when it is first connected to the document.

These mixins are used together so often that, for convenience, they are used to create an Elix component base class called ReactiveElement. There is nothing special about the ReactiveElement base class, and it can easily be recreated by applying the above mixins to HTMLElement:

const ReactiveElement = AttributeMarshallingMixin(


A simple increment/decrement component using ReactiveElement might look like this:

import { ReactiveElement, internal, template } from "elix";

class IncrementDecrement extends ReactiveElement {
  // This property becomes the initial value of this[internal.state] at constructor time.
  get [internal.defaultState]() {
    return { value: 0 };

  // Indicate what should update when state changes.
  [internal.render](changed) {

    // After the first render creates shadow elements, wire up event handlers.
    if (this[internal.firstRender]) {
      this[internal.ids].decrement.addEventListener("click", () => {
      this[internal.ids].increment.addEventListener("click", () => {

    // When the value changes, show the value as the span's text.
    if (changed.value) {
      this[internal.ids].value.textContent = this[internal.state].value;

  // Define an initial component template.
  get [internal.template]() {
    return template.html`
      <button id="decrement">-</button>
      <span id="value"></span>
      <button id="increment">+</button>

  // Provide a public property that gets/sets state.
  get value() {
    return this[internal.state].value;
  set value(value) {
    this[internal.setState]({ value });
Demo: A simple increment/decrement component defined with ReactiveElement

The internal.template property defines the template that ShadowTemplateMixin will use to populate the component's shadow root. The internal.render method then updates the DOM when state changes. In this case, when the value state member changes, the component will update the textContent of the span.

If you don't want to use an HTML template to define your component's initial DOM structure, or want to render state changes a different way, you might consider using the more fundamental ReactiveMixin directly.


Class hierarchy:

booleanAttributeValue(name, value) static method

Given a string value for a named boolean attribute, return true if the value is either: a) the empty string, or b) a case-insensitive match for the name.

This is native HTML behavior; see the MDN documentation on boolean attributes for the reasoning.

Given a null value, this return false. Given a boolean value, this return the value as is.


  • name: string
  • value: string|boolean|null

Defined by AttributeMarshallingMixin

defaultState property

The default state for the component. This can be extended by mixins and classes to provide additional default state.

Type: PlainObject

Defined by ReactiveMixin

ids property

A convenient shortcut for looking up an element by ID in the component's Shadow DOM subtree.

Example: if component's template contains a shadow element <button id="foo">, you can use the reference this[ids].foo to obtain the corresponding button in the component instance's shadow tree. The ids property is simply a shorthand for getElementById, so this[ids].foo is the same as this[shadowRoot].getElementById('foo').

Type: object

Defined by ShadowTemplateMixin

render(changed) method

Render the indicated changes in state to the DOM.

The default implementation of this method does nothing. Override this method in your component to update your component's host element and any shadow elements to reflect the component's new state. See the rendering example.

Be sure to call super in your method implementation so that your component's base classes and mixins have a chance to perform their own render work.


  • changed: ChangedFlagsdictionary of flags indicating which state members have changed since the last render

Defined by ReactiveMixin

renderChanges() method

Render any pending component changes to the DOM.

This method does nothing if the state has not changed since the last render call.

ReactiveMixin will invoke this method following a setState call; you should not need to invoke this method yourself.

This method invokes the internal render method, then invokes the rendered method.

Defined by ReactiveMixin

rendered(changed) method

Perform any work that must happen after state changes have been rendered to the DOM.

The default implementation of this method does nothing. Override this method in your component to perform work that requires the component to be fully rendered, such as setting focus on a shadow element or inspecting the computed style of an element. If such work should result in a change in component state, you can safely call setState during the rendered method.

Be sure to call super in your method implementation so that your component's base classes and mixins have a chance to perform their own post-render work.


  • changed: ChangedFlags

Defined by ReactiveMixin

setState(changes) method

Update the component's state by merging the specified changes on top of the existing state. If the component is connected to the document, and the new state has changed, this returns a promise to asynchronously render the component. Otherwise, this returns a resolved promise.


  • changes: PlainObjectthe changes to apply to the element's state

Returns: Promise - resolves when the new state has been rendered

Defined by ReactiveMixin

state property

The component's current state.

The returned state object is immutable. To update it, invoke internal.setState.

It's extremely useful to be able to inspect component state while debugging. If you append ?elixdebug=true to a page's URL, then ReactiveMixin will conditionally expose a public state property that returns the component's state. You can then access the state in your browser's debug console.

Type: PlainObject

Defined by ReactiveMixin

stateEffects(state, changed) method

Ask the component whether a state with a set of recently-changed fields implies that additional second-order changes should be applied to that state to make it consistent.

This method is invoked during a call to internal.setState to give all of a component's mixins and classes a chance to respond to changes in state. If one mixin/class updates state that it controls, another mixin/class may want to respond by updating some other state member that it controls.

This method should return a dictionary of changes that should be applied to the state. If the dictionary object is not empty, the internal.setState method will apply the changes to the state, and invoke this stateEffects method again to determine whether there are any third-order effects that should be applied. This process repeats until all mixins/classes report that they have no additional changes to make.

See an example of how ReactiveMixin invokes the stateEffects to ensure state consistency.


  • state: PlainObjecta proposal for a new state
  • changed: ChangedFlagsthe set of fields changed in this latest proposal for the new state

Returns: PlainObject

Defined by ReactiveMixin