Frontend / One-way vs two-way data binding

One-way vs two-way data binding

One input box and one variable: see which way data travels in one-way and two-way binding, how React, Vue, Angular and Svelte write each, and what the choice costs.

Beginner7 steps~6 min
Model — the data, held by the program
View — what the user sees and types into
Down — model to view: the screen is redrawn
Up — view to model: the user changed something
// THE SHORT VERSION

In 30 seconds

Data binding keeps the data in a program and what is shown on screen in step. One-way binding redraws the screen whenever the data changes; what the user does comes back as events that your code handles.

Two-way binding also writes what the user types straight back into the data, with one attribute. Underneath it is the same loop, with the handler written for you.

One-way is more code but easy to trace. Two-way is shorter, yet can hide who changed a value.

One-way vs two-way data binding, step by step

STEPS01 / 07

Syntax shown is React, Vue 3, Angular (ngModel comes from its forms module) and Svelte. Frameworks also differ in how they notice a change; only the direction of flow is drawn here.

STEP 01 / 07 One value, two places
THE MATH
model: name = 'Ada' view: input shows Ada
  1. Step 1: One value, two places. A name lives in two places: as data in the program (the model) and as text in an input box (the view). If either one changes, the other is out of date. Data binding is how a framework keeps them in step, and there are two directions to cover: model to view, and view to model.
  2. Step 2: One-way: data flows down. With one-way binding the framework handles a single direction: whenever the model changes, the view is redrawn from it. The program sets name to 'Ada', so the box and the greeting show Ada. Nothing the view does can alter the model by itself.
  3. Step 3: Events carry changes back. The user types m. In a one-way system the keystroke does not touch the model; it raises an event. A handler that you write receives the new text and stores it, and the framework then redraws the view from the model. The loop is closed, but every change passes through a line of your code.
  4. Step 4: Two-way: one attribute. Two-way binding ties the box and the data together with one attribute. The framework pushes the model into the view and also writes the user's typing straight back into the model. There is no handler to write, which is why it is popular for forms with many fields.
  5. Step 5: The same loop underneath. Two-way binding is not a different mechanism. Vue's v-model is shorthand for binding the value downward and listening for the input event; Angular's [(ngModel)] combines a property binding and an event binding in the same way. It is the one-way loop, with the handler written for you.
  6. Step 6: Where the bugs come from. Trouble starts when many parts of the screen are bound two-way to the same data. Any of them can change it, a change can trigger other bindings that change something else, and it becomes hard to say who set a value and why. With one-way flow, every change goes through one place that can be logged and checked.
  7. Step 7: Predictable or convenient. One-way flow costs more code but makes changes easy to trace. Two-way binding is shorter and convenient, at the price of writes you do not see. React offers only the one-way form; Vue, Angular and Svelte offer both, and commonly use two-way binding on form fields and one-way flow between components.
VALUES SO FAR
name'Ada'model
down····one-way
up····one-way
name····model
v-model····two-way
sugar····Vue
writers····risk
React····choice

Spotted a mistake on this page?