Created
June 6, 2017 20:09
-
-
Save casprwang/47262549af097c7dada7982a29a1c39f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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