Update CountDown.md
This commit is contained in:
@ -2,72 +2,76 @@
|
|||||||
|
|
||||||
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`
|
Use object destructuring to set defaults for the `hours`, `minutes` and `seconds` props.
|
||||||
- Each second, `tick()` is called. It updates the timer, decreasing the time by one second each time
|
Use the `React.useState()` hook to create the `time`, `paused` and `over` state variables and set their values to the values of the passed props, `false` and `false` respectively.
|
||||||
- If the `pause` button is clicked, `tick()` refrains from updating the time until the `resume` button is clicked
|
Create a method `tick`, that updates the value of `time` based on the current value (i.e. decreasing the time by one second).
|
||||||
- If the `restart` button is clicked, the timer starts over from step 1
|
If `paused` or `over` is `true`, `tick` will return immediately.
|
||||||
- If the time hits zero, the message 'Time's up!' is printed
|
Create a method `reset`, that resets all state variables to their initial states.
|
||||||
|
Use the the `React.useEffect()` hook to call the `tick` method every second via the use of `setInterval()` and use `clearInterval()` to cleanup when the component is unmounted.
|
||||||
|
Use a `<div>` to wrap a `<p>` element with the textual representation of the components `time` state variable, as well as two `<button>` elements that will pause/unpause and restart the timer respectively.
|
||||||
|
If `over` is `true`, the timer will display a message instead of the value of `time`.
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
function CountDown(props) {
|
function CountDown({ hours = 0, minutes = 0, seconds = 0 }) {
|
||||||
const [paused, setPaused] = useState(false);
|
const [paused, setPaused] = React.useState(false);
|
||||||
const [over, setOver] = useState(false);
|
const [over, setOver] = React.useState(false);
|
||||||
const [time, setTime] = useState({
|
const [time, setTime] = React.useState({
|
||||||
hours: parseInt(props.hours),
|
hours: parseInt(hours),
|
||||||
minutes: parseInt(props.minutes),
|
minutes: parseInt(minutes),
|
||||||
seconds: parseInt(props.seconds)
|
seconds: parseInt(seconds)
|
||||||
});
|
});
|
||||||
|
|
||||||
function tick() {
|
const tick = () => {
|
||||||
if (paused) {
|
if (paused || over) return;
|
||||||
return;
|
if (time.hours == 0 && time.minutes == 0 && time.seconds == 0)
|
||||||
}
|
|
||||||
|
|
||||||
if (time.hours == 0 && time.minutes == 0 && time.seconds == 0) {
|
|
||||||
setOver(true);
|
setOver(true);
|
||||||
} else if (time.minutes == 0 && time.seconds == 0) {
|
else if (time.minutes == 0 && time.seconds == 0)
|
||||||
setTime({
|
setTime({
|
||||||
hours: time.hours - 1,
|
hours: time.hours - 1,
|
||||||
minutes: 59,
|
minutes: 59,
|
||||||
seconds: 59
|
seconds: 59
|
||||||
});
|
});
|
||||||
} else if (time.seconds == 0) {
|
else if (time.seconds == 0)
|
||||||
setTime({
|
setTime({
|
||||||
hours: time.hours,
|
hours: time.hours,
|
||||||
minutes: time.minutes - 1,
|
minutes: time.minutes - 1,
|
||||||
seconds: 59
|
seconds: 59
|
||||||
});
|
});
|
||||||
} else {
|
else
|
||||||
setTime({
|
setTime({
|
||||||
hours: time.hours,
|
hours: time.hours,
|
||||||
minutes: time.minutes,
|
minutes: time.minutes,
|
||||||
seconds: time.seconds - 1
|
seconds: time.seconds - 1
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
function reset() {
|
const reset = () => {
|
||||||
setTime({
|
setTime({
|
||||||
hours: parseInt(props.hours),
|
hours: parseInt(hours),
|
||||||
minutes: parseInt(props.minutes),
|
minutes: parseInt(minutes),
|
||||||
seconds: parseInt(props.seconds)
|
seconds: parseInt(seconds)
|
||||||
});
|
});
|
||||||
setPaused(false);
|
setPaused(false);
|
||||||
setOver(false);
|
setOver(false);
|
||||||
}
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
React.useEffect(() => {
|
||||||
let timerID = setInterval(() => tick(), 1000);
|
let timerID = setInterval(() => tick(), 1000);
|
||||||
|
|
||||||
return () => clearInterval(timerID);
|
return () => clearInterval(timerID);
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<p>{`${time.hours.toString().padStart(2, '0')}:${time.minutes.toString().padStart(2, '0')}:${time.seconds.toString().padStart(2, '0')}`}</p>
|
<p>{`${time.hours
|
||||||
<div>{over ? "Time's up!" : ''}</div>
|
.toString()
|
||||||
<button onClick={() => setPaused(!paused)}>{paused ? 'RESUME' : 'PAUSE'}</button>
|
.padStart(2, "0")}:${time.minutes
|
||||||
<button onClick={() => reset()}>RESTART</button>
|
.toString()
|
||||||
|
.padStart(2, "0")}:${time.seconds.toString().padStart(2, "0")}`}</p>
|
||||||
|
<div>{over ? "Time's up!" : ""}</div>
|
||||||
|
<button onClick={() => setPaused(!paused)}>
|
||||||
|
{paused ? "Resume" : "Pause"}
|
||||||
|
</button>
|
||||||
|
<button onClick={() => reset()}>Restart</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -75,7 +79,11 @@ function CountDown(props) {
|
|||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<CountDown hours="1" minutes="45" seconds="0" />,
|
<CountDown hours="1" minutes="45" />,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<!-- tags: visual,state -->
|
||||||
|
|
||||||
|
<!-- expertise: 2 -->
|
||||||
|
|||||||
Reference in New Issue
Block a user