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>