From 2290c9f06f23a2c97ad086dff959da7d9d646f6e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 22 Feb 2019 20:13:46 +0200 Subject: [PATCH] Update CountDown.md --- snippets/CountDown.md | 80 ++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/snippets/CountDown.md b/snippets/CountDown.md index 83dd8cb43..985f35d27 100644 --- a/snippets/CountDown.md +++ b/snippets/CountDown.md @@ -2,72 +2,76 @@ Renders a countdown timer that prints a message when it reaches zero. -- 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 -- 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 time hits zero, the message 'Time's up!' is printed +Use object destructuring to set defaults for the `hours`, `minutes` and `seconds` props. +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. +Create a method `tick`, that updates the value of `time` based on the current value (i.e. decreasing the time by one second). +If `paused` or `over` is `true`, `tick` will return immediately. +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 `
` to wrap a `

` element with the textual representation of the components `time` state variable, as well as two ` - +

{`${time.hours + .toString() + .padStart(2, "0")}:${time.minutes + .toString() + .padStart(2, "0")}:${time.seconds.toString().padStart(2, "0")}`}

+
{over ? "Time's up!" : ""}
+ +
); } @@ -75,7 +79,11 @@ function CountDown(props) { ```jsx ReactDOM.render( - , + , document.getElementById('root') ); ``` + + + +