Created
March 6, 2016 03:40
-
-
Save franzejr/30590362d884a5c164d7 to your computer and use it in GitHub Desktop.
Counter ReactJS
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
import React, { Component, PropTypes } from 'react' | |
class Counter extends Component { | |
constructor(props) { | |
super(props) | |
this.incrementAsync = this.incrementAsync.bind(this) | |
this.incrementIfOdd = this.incrementIfOdd.bind(this) | |
} | |
incrementIfOdd() { | |
if (this.props.value % 2 !== 0) { | |
this.props.onIncrement() | |
} | |
} | |
incrementAsync() { | |
setTimeout(this.props.onIncrement, 1000) | |
} | |
render() { | |
const { value, onIncrement, onDecrement } = this.props | |
return ( | |
<p> | |
Clicked: {value} times | |
{' '} | |
<button onClick={onIncrement}> | |
+ | |
</button> | |
{' '} | |
<button onClick={onDecrement}> | |
- | |
</button> | |
{' '} | |
<button onClick={this.incrementIfOdd}> | |
Increment if odd | |
</button> | |
{' '} | |
<button onClick={this.incrementAsync}> | |
Increment async | |
</button> | |
</p> | |
) | |
} | |
} | |
Counter.propTypes = { | |
value: PropTypes.number.isRequired, | |
onIncrement: PropTypes.func.isRequired, | |
onDecrement: PropTypes.func.isRequired | |
} | |
export default Counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment