liveBook · Manning

Two-way binding

As explained in Build a Frontend Web Framework (From Scratch)

A synchronization technique in which changes to application state are reflected in the user interface and changes to the interface are reflected back in the state.

Two-way binding keeps application state and the DOM synchronized: state changes update the interface, while interface changes update state through events. section 6.3.3 section 6.4

A small input binding separates the mechanism into two responsibilities:

  • Render the current state through the input’s value attribute. section 6.3.3
  • Listen for input events and dispatch the new value so a reducer updates state. section 6.3.3

Binding a TODO input

Set value to state.currentTodo, then emit update-current-todo with target.value whenever the user types. The renderer applies the updated state back to the input. section 6.3.3

Listing 6.5 The CreateTodo() component (todos.js)
function CreateTodo({ currentTodo }, emit) {                #1
  return h('div', {}, [
    h('label', { for: 'todo-input' }, ['New TODO']),        #2
    h('input', {
      type: 'text',
      id: 'todo-input',
      value: currentTodo,          #3
      on: {
        input: ({ target }) => 
          emit('update-current-todo', target.value),     #4
        keydown: ({ key }) => { 
          if (key === 'Enter' && currentTodo.length >= 3) {  #5
            emit('add-todo')                    #6
          }
        },
      },
    }),
    h(
      'button',
      {
        disabled: currentTodo.length < 3,                   #7
        on: { click: () => emit('add-todo') },              #8
      },
      ['Add']
    ),
  ])
}

Inspect the complete CreateTodo component, including the bound input, event handler, Enter-key behavior, and Add button. section 6.3.3

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Defines the state-DOM flow and implements it with a value attribute, input listener, dispatched event, and reducer.
Examples
Uses a currentTodo text input and an editable TODO item to demonstrate state updates from typed values.
What it assumes
Assumes components receive state and emit functions, while demonstrating how the renderer and state manager cooperate.
What you can do after
Build it from scratch by binding an input value to state and dispatching input events that update the state.
How it connects
Connects application state, the DOM, event listeners, reducers, renderers, and state managers.

Also explained in

Compare how 2 books explain two-way binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.