Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is React's data-binding really one way? It seems like it is two way

This is from a couple React tutorials that I'm currently reading:

the State drives what the guys at Facebook call one-way reactive data flow, meaning that our UI will react to every change of state.

and

Typically UI’s have lots of state which makes managing state difficult. By re-rendering the virtual DOM every time any state change occurs, React makes it easier to think about what state your application is in. The process looks something like this, Signal to notify our app some data has changed→ Re-render virtual DOM -> Diff previous virtual DOM with new virtual DOM -> Only update real DOM with necessary changes.

The first quote seems to suggest that the data flow goes from React to the UI. But the second quote seems to suggest that it goes from the DOM to React which then re-renders the virtual DOM and the diff process than repaints the real DOM. This sounds a lot like Angular's two-way data binding.

Is this true? What am I missing? Is one-way reactive data flow just another name for Angular's two-way data binding?

like image 436
Jwan622 Avatar asked Sep 22 '15 08:09

Jwan622


People also ask

Is React one way data binding or two-way binding?

In React, data flows one way: from owner to child. We think that this makes your app's code easier to understand. You can think of it as “one-way data binding.” However, there are lots of applications that require you to read some data and flow it back into your program.

What is a two-way data binding?

Two-way binding gives components in your application a way to share data. Use two-way binding to listen for events and update values simultaneously between parent and child components.

What is true about one way data binding?

One-way data binding will bind the data from the component to the view (DOM) or from view to the component. One-way data binding is unidirectional. You can only bind the data from component to the view or from view to the component.

Can we have 2 way data binding in React?

To be short, in React, there's no two-way data-binding.


2 Answers

I think it's necessary to make a distinction between React and Flux, both of which implement a uni-directional data flow. I'll only touch on React here.

In Angular a change to the DOM will directly mutate the value of bound scope variables in your controller thanks to angular's two-way data binding and the digest loop. It is two way because, of course, any change in your controller is also directly reflected in the view attached to it.

Think of Angular as Controller <--> View

In React a component always renders its DOM based on its props and internal state. A React component can listen to event being fired in the DOM it rendered, which might sound a bit like two-way data flow on the face of it but there is an important difference: An event fired on the DOM does not directly update the internal state of the component. The component must listen to the event and then explicitly update the component with the new state. This change of state then triggers the DOM to be re-rendered (if it needs to). In this way the components props and state are the single source of truth and always drive the DOM output. Of course changes in the DOM can affect the component but it is done in an indirect manner.

Think of React as Component State --> DOM --> New Component State --> New DOM

like image 67
djskinner Avatar answered Nov 16 '22 01:11

djskinner


Angular's two way of binding's essence is that the view is bound both to the model and the user's input.

Angular's two way of binding

With React, the view is bound only to the state and a user's input cannot directly change the view, but triggers the component's own methods to update its state and Reacts job is to rerender the view so it reflects the state.

React's one way binding

like image 31
Marta Avatar answered Nov 16 '22 00:11

Marta