One way data binding is very simple, except in React we rarely use the word binding to refer how the data flows.
const fn = (a) => { return ... }
If a value is provided as a
, we'll use that value in the function scope. The above is programming 101.
<Title name={"Hello"} />
The above line doesn't mean anything would magically happen other than the fact that "Hello" is sent to Title function and set one prop to "Hello", if you insist to use the word bind here, that's where the bind happens.
Whether you want to use this prop to display or wire with another state or whatever, you have to code yourself! There's no other magic. Btw, this is called props in React. And props is more or less a function's input argument coded in an object format. So the more accurate definition of this "bind" in React should be called assignment. In React source code, you'll see something very quickly after the element is created.
element.props = { name: "Hello" }
And believe it or not, there's no other code in React that does anything to do with this "bind" later on.
Example
Use the input example,
<input value={value} onChange={onChange} />
If we give a value
to input
, the input would pick up the value to display it. If you change the value, you are intending to change the display.
Why does the value
change? It can't by default. You have to change it by either listening to a system event like onChange
or some business logic like setTimeout
or whatever way you can ever imagine. But the change is an action, you perform the action therefore you can handle the action by changing the value. I guess this is where the "One-way" comes from. Essentially nothing is free.
Confusion
What gets us confused is that DOM element has its own state or properties. For example, we can do el.textContent="abc"
without using React.
<input />
If we just code like this, we still see the value on screen changes after we type in anything. But this behavior has nothing to do with React, it's the DOM element functionalities. React refers to the first version as controlled element. Essentially React overwrites the DOM way.
NOTE
To be honest, only after I stopped using the word "binding" for these cases, I started to understand what they are. But that could be just me.