|
@@ -1,45 +1,18 @@
|
|
|
-import React from "react";
|
|
|
+import React, { useEffect, useState } from "react";
|
|
|
import ms from "pretty-ms";
|
|
|
|
|
|
-class RoundTimer extends React.Component {
|
|
|
- constructor(props) {
|
|
|
- super(props);
|
|
|
- const { seconds } = this.props;
|
|
|
- this.state = {
|
|
|
- running: false,
|
|
|
- remaining: seconds,
|
|
|
+export default ({ seconds, onTimeout }) => {
|
|
|
+ const [remaining, setRemaining] = useState(seconds);
|
|
|
+ useEffect(() => {
|
|
|
+ const countDown = () => {
|
|
|
+ setRemaining(remaining - 1)
|
|
|
+ if (remaining <= 0) {
|
|
|
+ onTimeout();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+ const timer = setInterval(countDown, 1000);
|
|
|
+ return () => { clearInterval(timer) };
|
|
|
+ });
|
|
|
|
|
|
- componentDidMount() {
|
|
|
- this.timer = setInterval(() => this.countDown(), 1000);
|
|
|
- }
|
|
|
-
|
|
|
- componentWillUnmount() {
|
|
|
- clearInterval(this.timer);
|
|
|
- }
|
|
|
-
|
|
|
- countDown() {
|
|
|
- const { remaining } = this.state;
|
|
|
-
|
|
|
- this.setState({ remaining: remaining - 1 });
|
|
|
-
|
|
|
- if (remaining <= 0) {
|
|
|
- const { onTimeout } = this.props;
|
|
|
- clearInterval(this.timer);
|
|
|
- onTimeout();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- const { remaining } = this.state;
|
|
|
- const { style } = this.props;
|
|
|
- return (
|
|
|
- <div style={{ display: "flex", justifyContent: "center", alignItems: "center", ...style }}>
|
|
|
- {remaining > 0 ? <p>Time: {ms(remaining * 1000)}</p> : <p>Time's up!</p>}
|
|
|
- </div>
|
|
|
- );
|
|
|
- }
|
|
|
+ return remaining > 0 ? <p>Time: {ms(remaining * 1000)}</p> : <p>Time's up!</p>
|
|
|
}
|
|
|
-
|
|
|
-export default RoundTimer;
|