Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2017 15:22
Show Gist options
  • Save anonymous/d02a9c8851d73c990954403e28cc35b7 to your computer and use it in GitHub Desktop.
Save anonymous/d02a9c8851d73c990954403e28cc35b7 to your computer and use it in GitHub Desktop.
React Counter
<div id="root">
<!-- This element's contents will be replaced with your component. -->
</div>
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.increment = this.increment.bind(this);
this.decrement = this.decrement.bind(this);
}
render() {
return (
<div>
<h3> Counter </h3>
<div onClick={this.increment}> + </div>
<div> {this.state.count} </div>
<div onClick={this.decrement}> - </div>
</div>
)
}
decrement() {
this.setState({count: --this.state.count});
}
increment() {
this.setState({count: ++this.state.count});
}
}
ReactDOM.render(
<Counter />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment