Skip to content

Instantly share code, notes, and snippets.

@adithyamaheshb
Created June 7, 2018 05:37
Show Gist options
  • Save adithyamaheshb/a2a52a5a90e1fb6e556f9a4a981b839d to your computer and use it in GitHub Desktop.
Save adithyamaheshb/a2a52a5a90e1fb6e556f9a4a981b839d to your computer and use it in GitHub Desktop.
Increment/Decrement Counter
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
clicks: 0,
show: true
};
this.IncrementItem = this.IncrementItem.bind(this);
this.DecrementItem = this.DecrementItem.bind(this);
this.ToggleClick = this.ToggleClick.bind(this);
}
IncrementItem() {
this.setState({ clicks: this.state.clicks + 1 });
}
DecrementItem() {
this.setState({ clicks: this.state.clicks - 1 });
}
ToggleClick() {
this.setState({ show: !this.state.show });
}
render() {
return (
<div>
<button onClick={this.IncrementItem}>Increment</button>
<button onClick={this.DecrementItem}>Decrement</button>
<button onClick={this.ToggleClick}>
{ this.state.show ? 'Hide number' : 'Show number' }
</button>
{ this.state.show ? <h2>{this.state.clicks}</h2> : '' }
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment