By Henk Bakker
var LikeButton = React.createClass({
getInitialState: function() {
return {liked: false};
},
handleClick: function(event) {
this.setState({liked: !this.state.liked});
},
render: function() {
var text = this.state.liked ? 'like' : 'haven\'t liked';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
}
});
React.render(
<LikeButton />,
document.getElementById('example')
);
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
“Reaction”to React
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
“Ugly.”
“Separation of concerns!!”
“React is a templating language.”
“Reaction”to React
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
*If* Components are truly self-contained
No framework: Any component can communicate with any other component
Backbone: Pub-sub item.on('change:name', function() {…
Angular: 2-way data binding and $digest loop$scope.name = …
React: 1-way data flow
boolean shouldComponentUpdate(object nextProps, object nextState)
var LikeButton = React.createClass({
getInitialState: function() {
return {liked: false};
},
handleClick: function(event) {
this.setState({liked: !this.state.liked});
},
render: function() {
var text = this.state.liked ? 'like' : 'haven\'t liked';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
}
});
var LikeButton = React.createClass({displayName: "LikeButton",
getInitialState: function() {
return {liked: false};
},
handleClick: function(event) {
this.setState({liked: !this.state.liked});
},
render: function() {
var text = this.state.liked ? 'like' : 'haven\'t liked';
return (
React.createElement("p", {onClick: this.handleClick},
"You ", text, " this. Click to toggle."
)
);
}
});