From f384fc0ee35fb19a5af3a2faabd0a63a19a87ab4 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 6 Sep 2020 14:11:58 +0300 Subject: [PATCH] Update Alert --- snippets/Alert.md | 58 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/snippets/Alert.md b/snippets/Alert.md index e5edcee2e..4f867ba85 100644 --- a/snippets/Alert.md +++ b/snippets/Alert.md @@ -1,15 +1,15 @@ --- title: Alert -tags: components,beginner,state,effect +tags: components,state,effect,beginner --- Creates an alert component with `type` prop. - Define appropriate CSS styles and animations for the component's elements. -- Use the `React.setState()` hook to create the `isShown` and `isLeaving` state variables and set their values to `false`. +- Use the `React.useState()` hook to create the `isShown` and `isLeaving` state variables and set their values to `false`. - Define `timeoutId` to keep the timer instance for clearing on component unmount. -- Use the `React.setEffect()` hook to update the value of `isShown` to `true` and clear interval by using `timeoutId` when component is unmounted. -- Define `closeNotification` function to set the component is removed from DOM by displaying fading out animation and set `isShown` to `false` via `setTimeout()`. +- Use the `React.useEffect()` hook to update the value of `isShown` to `true` and clear interval by using `timeoutId` when component is unmounted. +- Define `closeAlert` function to set the component is removed from DOM by displaying fading out animation and set `isShown` to `false` via `setTimeout()`. - Define the component, which renders the alert component with user defined `message` and a close button to remove the component from DOM. ```css @@ -64,36 +64,36 @@ Creates an alert component with `type` prop. ``` ```jsx -function Notification(props) { - const [isShown, setIsShown] = React.useState(false); - const [isLeaving, setIsLeaving] = React.useState(false); +const Alert = ({ isDefaultShown = false, timeout = 250, type, message }) => { + const [isShown, setIsShown] = React.useState(isDefaultShown); + const [isLeaving, setIsLeaving] = React.useState(false); - let timeoutId = null; + let timeoutId = null; - React.useEffect(() => { - setIsShown(true); - return () => { - clearTimeout(timeoutId); - } - }, [props.isShown, props.timeout, timeoutId]); + React.useEffect(() => { + setIsShown(true); + return () => { + clearTimeout(timeoutId); + }; + }, [isDefaultShown, timeout, timeoutId]); - const closeNotification = () => { - setIsLeaving(true); - timeoutId = setTimeout(() => { - setIsLeaving(false); - setIsShown(false); - }, 250) - } + const closeAlert = () => { + setIsLeaving(true); + timeoutId = setTimeout(() => { + setIsLeaving(false); + setIsShown(false); + }, timeout); + }; - return isShown && ( -
-
- ) -} + return isShown && ( +
+
+ ); +}; ``` ```jsx -ReactDOM.render(, document.getElementById('root')); +ReactDOM.render(, document.getElementById('root')); ```