-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCounter.js
More file actions
55 lines (48 loc) · 1.11 KB
/
Counter.js
File metadata and controls
55 lines (48 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
import React, {Component} from 'react'
import PropTypes from 'prop-types'
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.increment()
}
}
incrementAsync() {
setTimeout(this.props.increment, 1000)
}
render() {
const {value, increment, decrement} = this.props;
return (
<p>
Clicked: {value} times
{' '}
<button onClick={increment}>
+
</button>
{' '}
<button onClick={decrement}>
-
</button>
{' '}
<button onClick={this.incrementIfOdd}>
Increment if odd
</button>
{' '}
<button onClick={this.incrementAsync}>
Increment async
</button>
</p>
)
}
}
Counter.propTypes = {
value: PropTypes.number.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired
};
export default Counter