Skip to content

Instantly share code, notes, and snippets.

@osmelmora
Created June 20, 2016 21:32
Show Gist options
  • Save osmelmora/436917ea0d08b8ea97281b07a84248ca to your computer and use it in GitHub Desktop.
Save osmelmora/436917ea0d08b8ea97281b07a84248ca to your computer and use it in GitHub Desktop.
var Component = React.createClass({
getDefaultProps: function() {
console.log('getDefaultProps');
return {
title: "Basic counter!!!",
step: 1
}
},
getInitialState: function() {
console.log('getInitialState');
return {
count: (this.props.initialCount || 0)
};
},
render: function() {
console.log('render');
var step = this.props.step;
return (
<div>
<h1>{this.props.title}</h1>
<div>{this.state.count}</div>
<input type='button' value='+' onClick={this.updateCounter.bind(this, step)} />
<input type='button' value='-' onClick={this.updateCounter.bind(this, -step)} />
</div>
);
},
updateCounter: function(value) {
var newCount = this.state.count + value;
this.setState({count: newCount});
},
propTypes: {
title: React.PropTypes.string,
initialCount: React.PropTypes.number,
step: React.PropTypes.number
}
});
ReactDOM.render(
React.createElement(Component, {initialCount: 5, step: 2}),
document.getElementById('app-container')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment