convert to use hooks
This commit is contained in:
@ -3,86 +3,73 @@
|
|||||||
Renders a countdown timer that prints a message when it reaches zero.
|
Renders a countdown timer that prints a message when it reaches zero.
|
||||||
|
|
||||||
- The timer starts automatically with the initial value passed through the `props`
|
- The timer starts automatically with the initial value passed through the `props`
|
||||||
- Each second, `tick()` is called. It updates the timer, decreasing the time by one second each time.
|
- Each second, `tick()` is called. It updates the timer, decreasing the time by one second each time
|
||||||
- If the `pause` button is clicked, `tick()` refrains from updating the time until the `resume` button is clicked.
|
- If the `pause` button is clicked, `tick()` refrains from updating the time until the `resume` button is clicked
|
||||||
- If the `restart` button is clicked, the timer starts over from step 1
|
- If the `restart` button is clicked, the timer starts over from step 1
|
||||||
- If the time hits zero, the message 'Time's up!' is printed
|
- If the time hits zero, the message 'Time's up!' is printed
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
class CountDown extends React.Component {
|
function CountDown(props) {
|
||||||
constructor(props) {
|
const [paused, setPaused] = useState(false);
|
||||||
super(props);
|
const [over, setOver] = useState(false);
|
||||||
this.state = {
|
const [time, setTime] = useState({
|
||||||
paused: false,
|
hours: parseInt(props.hours),
|
||||||
done: false,
|
minutes: parseInt(props.minutes),
|
||||||
s: parseInt(this.props.seconds),
|
seconds: parseInt(props.seconds)
|
||||||
m: parseInt(this.props.minutes),
|
});
|
||||||
h: parseInt(this.props.hours)
|
|
||||||
};
|
|
||||||
|
|
||||||
this.tick = this.tick.bind(this);
|
function tick() {
|
||||||
this.pause = this.pause.bind(this);
|
if (paused) {
|
||||||
this.reset = this.reset.bind(this);
|
|
||||||
|
|
||||||
this.timerID = setInterval(this.tick, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
tick() {
|
|
||||||
if (this.state.paused) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.state.s === 0 && this.state.m === 0 && this.state.h === 0) {
|
if (time.hours == 0 && time.minutes == 0 && time.seconds == 0) {
|
||||||
clearInterval(this.timerID);
|
setOver(true);
|
||||||
this.setState({
|
} else if (time.minutes == 0 && time.seconds == 0) {
|
||||||
done: true
|
setTime({
|
||||||
|
hours: time.hours - 1,
|
||||||
|
minutes: 59,
|
||||||
|
seconds: 59
|
||||||
|
});
|
||||||
|
} else if (time.seconds == 0) {
|
||||||
|
setTime({
|
||||||
|
hours: time.hours,
|
||||||
|
minutes: time.minutes - 1,
|
||||||
|
seconds: 59
|
||||||
});
|
});
|
||||||
} else if (this.state.m === 0 && this.state.s === 0) {
|
|
||||||
this.setState(state => ({
|
|
||||||
h: state.h - 1,
|
|
||||||
m: 59,
|
|
||||||
s: 59
|
|
||||||
}));
|
|
||||||
} else if (this.state.s === 0) {
|
|
||||||
this.setState(state => ({
|
|
||||||
m: state.m - 1,
|
|
||||||
s: 59
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
this.setState(state => ({
|
setTime({
|
||||||
s: state.s - 1
|
hours: time.hours,
|
||||||
}))
|
minutes: time.minutes,
|
||||||
|
seconds: time.seconds - 1
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pause() {
|
function reset() {
|
||||||
this.setState(state => ({
|
setTime({
|
||||||
paused: !state.paused
|
hours: parseInt(props.hours),
|
||||||
}))
|
minutes: parseInt(props.minutes),
|
||||||
}
|
seconds: parseInt(props.seconds)
|
||||||
|
|
||||||
reset() {
|
|
||||||
clearInterval(this.timerID);
|
|
||||||
this.setState({
|
|
||||||
paused: false,
|
|
||||||
done: false,
|
|
||||||
s: parseInt(this.props.seconds),
|
|
||||||
m: parseInt(this.props.minutes),
|
|
||||||
h: parseInt(this.props.hours)
|
|
||||||
});
|
});
|
||||||
this.timerID = setInterval(this.tick, 1000);
|
setPaused(false);
|
||||||
|
setOver(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
useEffect(() => {
|
||||||
|
let timerID = setInterval(() => tick(), 1000);
|
||||||
|
|
||||||
|
return () => clearInterval(timerID);
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>{this.state.h.toString().padStart(2, '0') + ':' + this.state.m.toString().padStart(2, '0') + ':' + this.state.s.toString().padStart(2, '0')}</h1>
|
<p>{`${time.hours.toString().padStart(2, '0')}:${time.minutes.toString().padStart(2, '0')}:${time.seconds.toString().padStart(2, '0')}`}</p>
|
||||||
<button onClick={this.pause}>{this.state.paused ? 'RESUME' : 'PAUSE'}</button>
|
<div>{over ? "Time's up!" : ''}</div>
|
||||||
<button onClick={this.reset}>RESTART</button>
|
<button onClick={() => setPaused(!paused)}>{paused ? 'RESUME' : 'PAUSE'}</button>
|
||||||
<h1>{this.state.done ? "Time's up!" : ''}</h1>
|
<button onClick={() => reset()}>RESTART</button>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user