Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unidirectional data flow in React

Tags:

reactjs

vue.js

React has the notion of unidirectional data flow in contrast with two-way data binding from other frameworks e.g. Vue.js.

I've made the following snippets to sense the essential difference, then it seems that the unidirectional mandates the application to propagate any state transition, whereas the two-way conventionally does it for you.

Hopefully, I may have mistaken the gist here, so could you help clarify anything more fundamentally divergent between the duo?

var MyComponent = React.createClass({
  getInitialState: function () { 
    return {
      one: 'Hello', 
      two: 'world', 
      three: ''
    } 
  },
  handleValueOneChange: function (e) {
    this.setState({one: e.target.value})
  },
  handleValueTwoChange: function (e) {
    this.setState({two: e.target.value})
  },
  render: function () { 
    return (
      <div>
        <h1>
        {this.state.one + ' ' + this.state.two}
        </h1>
        <input value={this.state.one} 
          onChange={this.handleValueOneChange} />
        <input value={this.state.two} 
          onChange={this.handleValueTwoChange} />
      </div>
    )   
  }
})

ReactDOM.render(
  <MyComponent />, document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app"/>

new Vue({
  el: '#app', 
  data: {
    one: 'Hello',
    two: 'world'
  },
  computed: {
    three: function() { return this.one + ' ' + this.two }
  }
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="app">
  <h1>{{ three }}</h1>
  <input v-model="one" />
  <input v-model="two" />
</div>
like image 650
sof Avatar asked Dec 24 '22 02:12

sof


1 Answers

That's correct. I'm not familiar with vue.js but angular also does something similar with two way data binding. Now, React might look too tedious when you need it because of all the extra code that you need to write (at least it feels like that).

But what I have noticed is that two-way data binding is not needed for most of the pages and most of html components you write. They are essential for form based elements and that's mostly it. And most of the pages are not form based. I think the angular team has realized this fact and how much it impacts the performance of the application, and hence they introduced constructs for one-way data binding starting with version 1.3.

So, overall, it depends on what you are trying to do and choose the best solution. For example, you could be writing an application that is totally based on forms (I wrote one hug application that way) and two-way data binding will be of immense help. But most others, one-way is good enough.

Here are other SO posts that might be of interest for you:

Can anyone explain the difference between Reacts one-way data binding and Angular's two-way data binding

What is two way binding?

like image 151
KumarM Avatar answered Jan 08 '23 13:01

KumarM