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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Props vs stateWhat a component is given, what it owns, and where shared data should live. Beginner
- Signals and fine-grained reactivityValues that know who reads them, so one change updates exactly what depends on it. Intermediate
- MVVMModel, view and view model: a screen kept in step by bindings, with logic you can test. Intermediate
Spotted a mistake on this page?