diff --git a/snippets/CountDown.md b/snippets/CountDown.md new file mode 100644 index 000000000..985f35d27 --- /dev/null +++ b/snippets/CountDown.md @@ -0,0 +1,89 @@ +### CountDown + +Renders a countdown timer that prints a message when it reaches zero. + +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 ` + +

+ ); +} +``` + +```jsx +ReactDOM.render( + , + document.getElementById('root') +); +``` + + + +