liveBook · Manning

Two-way binding

As explained in Web Component Development with Modern Libraries and Tooling

A data-binding relationship in which changes synchronize between a data value and its bound user-interface representation.

Two-way binding keeps a component property and something connected to it in sync in both directions. A property can supply a value to an input or child component; a change made through that input or child can then update the property. section 8.7 section 9.8

Sliders that recalculate an area

An area calculator binds range inputs to width and height. Moving either slider updates its component property; the displayed value changes, and a computed area is recalculated. In wrec, the slider template uses `value:input="this.${property}"` for the binding. section 8.7

Listing 8.6 makeSlider function
function makeSlider(label: string, property: string) {
  return html`
    <div>
      <label for="${property}">${label}</label>
      <input
        id="${property}"
        type="range"
        min="1"
        max="10"
        value:input="this.${property}"
      />
      <span>this.${property}</span>
    </div>
  `;
}

The slider helper shows the input binding beside the label and displayed property value. section 8.7

The two directions matter. Lit’s `@property()` makes an attribute set the corresponding property and updates rendered output, but changing the property does not automatically change the attribute. Use `@property({ reflect: true })` when the attribute must also follow property changes. That attribute–property relationship is distinct from binding a user-editable input to a property. section 5.2 section 8.7

Binding can also connect components. A wrec parent passes its color property to both a greeting component and a radio group. Changing the selected radio value updates the shared color, so the greeting changes too. This automatic synchronization reduces the event-handling code needed by the radio-group implementation. FAST’s template system also includes two-way data binding, though no binding syntax is specified here. section 9.8 section 8.10 section 7.1

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Explains binding through declarations and visible updates, without detailing binding-engine internals.
Examples
Uses an area calculator with sliders, a color radio group, and a greeting component sharing its color.
What it assumes
Assumes familiarity with HTML inputs and component properties; demonstrates the bindings with small web-component examples.
What you can do after
Apply it via wrec to synchronize inputs or child components, and configure Lit attribute reflection when needed.
How it connects
Connects reactive properties, attribute reflection, computed properties, HTML inputs, and parent–child components.

Also explained in

Compare how 3 books explain two-way binding →

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.