Skip to content

Instantly share code, notes, and snippets.

@csbuja
Created October 17, 2014 20:26
Show Gist options
  • Save csbuja/8a0165d0dea6fdef6481 to your computer and use it in GitHub Desktop.
Save csbuja/8a0165d0dea6fdef6481 to your computer and use it in GitHub Desktop.
This is an example of how to build a counter with React.
/** @jsx React.DOM */
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' : 'unlike';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p> a
);
}
});
var CashCounter = React.createClass({
getInitialState: function() {
return {cashAmount: 0};
},
handleSubmit: function(){
var text = document.getElementById("thebox").value;
var numba = parseFloat(text)
if(!numba) return;
var num = numba + parseFloat(this.state.cashAmount);
var theVal = num.toString();
this.setState({cashAmount: theVal});
},
render: function() {
return (
<div>
<p > You have gathered {this.state.cashAmount} dollars. That's way more than Amer!</p>
<input type="textbox" id="thebox" />
<input type="button" id="submit" value="submit" onClick={this.handleSubmit}/>
</div>
);
}
});
React.renderComponent(
<CashCounter />,
document.getElementById('hi')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment