Skip to content

Instantly share code, notes, and snippets.

@haldarmahesh
Last active January 3, 2019 01:23
Show Gist options
  • Select an option

  • Save haldarmahesh/2d3500d1813f2820ae92bc74e1902228 to your computer and use it in GitHub Desktop.

Select an option

Save haldarmahesh/2d3500d1813f2820ae92bc74e1902228 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
export default class Counter extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
}
this.handleCounter = this.handleCounter.bind(this);
}
handleCounter(value) {
this.setState({
counter: value
});
}
render() {
return (
<div>
<div>
<button onClick={() => this.handleCounter(this.state.counter + 1)}> + Increment</button>
<button onClick={() => this.handleCounter(this.state.counter - 1)}> - Decrement</button>
</div>
<div>
Current: {this.state.counter}
</div>
</div>
)
}
}
@olee
Copy link
Copy Markdown

olee commented Jan 3, 2019

Also there is no need for a handleCounter method if you already use a lambda - you could just directly call setState and make the code a lot smaller - this example really is a bad one for comparing classes to hooks >_<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment