-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCountdown.jsx
54 lines (46 loc) · 1.2 KB
/
Countdown.jsx
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
import React from 'react';
import Clock from 'Clock';
import CountdownForm from 'CountdownForm';
class Countdown extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
countdownStatus: 0
};
}
componentDidUpdate(prevProps, prevState) {
if (this.state.countdownStatus !== prevState.countdownStatus) {
this.tick();
}
}
componentWillUnmount() {
clearInterval(this.timer);
}
tick() {
this.timer = setInterval(() => {
this.setState(oldState => {
const newCount = this.state.count - 1;
return {
count: newCount >= 0 ? newCount : 0
};
});
}, 1000);
}
handleSetCountdownTime(seconds) {
this.setState({
count: seconds,
countdownStatus: 1
});
}
render() {
var {count} = this.state;
return (
<div>
<Clock timeInSeconds={count}/>
<CountdownForm onSetCountdownTime={this.handleSetCountdownTime.bind(this)}/>
</div>
);
}
}
export default Countdown;