简体   繁体   English

在 React 中,单击按钮时,显示警报 5 秒,然后隐藏警报

[英]In React, on button click, display alert for 5 seconds, then hide alert

We are using this CSS which is for an animation where the div (or for any element using the animate class) disappears after 4 seconds.我们正在使用这个 CSS 动画,其中 div(或任何使用animate类的元素)在 4 秒后消失。

@keyframes FadeAnimation {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
.animate {
  animation: FadeAnimation 4s ease-in .1s forwards;
}

In react, we have a button that can be clicked, and when the button is clicked, the animate class is added to our Alert div (which was previously hidden):在react中,我们有一个可以点击的按钮,当点击按钮时, animate类被添加到我们的Alert div(之前是隐藏的):

const [isUpdating, setIsUpdating] = useState(false)
const [showAlert, setShowAlert] = useState(false)
const handleButtonClick = async () => {
    setIsUpdating(true);

    // Axios request to update user info
    try {
        const updateUserObj = { fullName, email };
        const userId = ...;
        const updateResponse = await Axios.put(`/users/update/${userId}`, updateUserObj);
        
        // Set States Following Successful Login
        setIsUpdating(false);
        setShowAlert(true); // setting to true to display hte alert
    } catch (err) {
        setIsUpdating(false);
    }
};

return (
    <Button onClick={handleButtonClick}>
        {`${isUpdating ? 'Updating...' : 'Submit Button'}`}
    />
    <Alert
        className={`modal-alert ${showAlert ? 'animate' : ''}`}
        variant='success'
    >
        Your profile was updated
    </Alert>);
)

This button click handles submitting a form which updates info for a user in our database, and it also displays an alert by updating the showAlert state to true, which adds animate as a class on the alert.此按钮单击处理提交更新我们数据库中用户信息的表单,它还通过将showAlert状态更新为 true 来显示警报,这将animate作为类添加到警报中。

Our problem is that this approach only works for the first click of the button, but not subsequent clicks.我们的问题是这种方法只适用于第一次点击按钮,但不适用于后续点击。 When the button is first clicked, showAlert is set to true , and nothing happens that ever turns this back to false .当第一次单击按钮时, showAlert设置为true ,并且不会发生任何事情将其转回false The CSS handles hiding the alert after 4 seconds, but the button is no longer usable. CSS 处理在 4 秒后隐藏警报,但按钮不再可用。

 // Get a hook function const {useState} = React; const Example = ({title}) => { const [isUpdating, setIsUpdating] = useState(false) const [showAlert, setShowAlert] = useState(false) const handleButtonClick = () => { setIsUpdating(true); setIsUpdating(false); setShowAlert(true); // setting to true to display hte alert }; let alertClass = showAlert ? 'modal-alert animate' : 'modal-alert'; let alertStyles = showAlert ? {} : { opacity: 0, visibility: 'hidden', pointerEvents: 'none' }; return ( <div> <div className='submit-button' onClick={handleButtonClick} > Submit Button </div> <div className={alertClass} style={alertStyles} variant='success' > Your profile was updated </div> </div> ) }; // Render it ReactDOM.render( <Example />, document.getElementById("react") );
 .modal-alert { border: 1px solid #222222; } .submit-button { cursor: pointer; background: blue; border-radius: 5px; padding: 10px 15px; color: white; font-size: 1em; &:hover { background: darkblue; } } @keyframes FadeAnimation { 0% { opacity: 1; visibility: visible; } 100% { opacity: 0; visibility: hidden; } } .animate { animation: FadeAnimation 4s ease-in .1s forwards; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>

You can make use of onAnimationEnd synthetic event to modify your state back:您可以使用onAnimationEnd合成事件来修改您的状态:

<Alert
  className={`modal-alert ${showAlert ? 'animate' : ''}`}
  variant='success'
  onAnimationEnd={() => setShowAlert(false)}
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM