Created
January 26, 2018 13:29
-
-
Save rajat1saxena/5414f7e58d2f1bee3c57aa24257c587e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Counter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {count: this.props.start || 0} | |
// the following bindings are necessary to make `this` work in the callback | |
this.inc = this.inc.bind(this); | |
this.dec = this.dec.bind(this); | |
} | |
inc() { | |
this.setState({ | |
count: this.state.count + 1 | |
}); | |
} | |
dec() { | |
this.setState({ | |
count: this.state.count - 1 | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<button onClick={this.inc}>+</button> | |
<button onClick={this.dec}>-</button> | |
<div>{this.state.count}</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment