Skip to content

Instantly share code, notes, and snippets.

@casprwang
Created June 6, 2017 20:09
Show Gist options
  • Save casprwang/47262549af097c7dada7982a29a1c39f to your computer and use it in GitHub Desktop.
Save casprwang/47262549af097c7dada7982a29a1c39f to your computer and use it in GitHub Desktop.
import React from "react"
// import Link from "gatsby-link"
// import get from "lodash/get"
// import Helmet from "react-helmet"
// import include from "underscore.string/include"
export default class BadCounter extends React.Component{
constructor(props){
super(props);
this.state = {count : 0}
this.incrementCount = this.incrementCount.bind(this)
}
incrementCount(){
this.setState({count : this.state.count + 1})
this.setState({count : this.state.count + 1})
}
render(){
return <div>
<button onClick={this.incrementCount}>Increment</button>
<div>{this.state.count}</div>
</div>
}
}
class GoodCounter extends React.Component{
constructor(props){
super(props);
this.state = {count : 0}
this.incrementCount = this.incrementCount.bind(this)
}
incrementCount(){
this.setState((prevState, props) => ({
count: prevState.count + 1
}));
this.setState((prevState, props) => ({
count: prevState.count + 1
}));
}
render(){
return <div>
<button onClick={this.incrementCount}>Increment</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