Skip to content

Instantly share code, notes, and snippets.

@balazimichal
Last active July 8, 2018 19:16
Show Gist options
  • Save balazimichal/52b22128c4b65a1d5f7a2db25645dff8 to your computer and use it in GitHub Desktop.
Save balazimichal/52b22128c4b65a1d5f7a2db25645dff8 to your computer and use it in GitHub Desktop.
react-simple-count
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props)
this.addCount = this.addCount.bind(this)
this.deductCount = this.deductCount.bind(this)
}
state = {
count: 0
}
addCount() {
this.setState((prevState) => ({
count: prevState.count + 1
}))
}
deductCount() {
this.setState((prevState) => ({
count: prevState.count - 1
}))
}
render() {
return (
<div>
{JSON.stringify(this.state)}
<h1>{this.state.count}</h1>
<button onClick={this.addCount}>+1</button>
<button onClick={this.deductCount}>-1</button>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment