简体   繁体   English

模态 reactjs 关闭时间

[英]modal reactjs close time

I need my modal to close after a certain time, how can I do this?我需要我的模态在一定时间后关闭,我该怎么做? this is what i have done so far这就是我到目前为止所做的

import React from 'react';
import IconcloseModal from './../../assets/icon/icon-cerrar-modal.png';
import './Modal.css'

const Modal = ({ typeModal, textModal, showModal, positionModal, closeModal }) => {
    let titleModal = '';
    if (typeModal === 'success')
        titleModal = '¡Bien hecho!';
    else if (typeModal === 'warning')
        titleModal = '¡Cuidado!';
    return (
        <div>
            <div className={(showModal) ? 'notify ' + positionModal + ' do-show' : 'notify ' + positionModal}
                data-notification-status={typeModal}>
                <div className="content-warning">
                    <h3>{titleModal}</h3>
                    <p> {textModal}</p>
                </div>
                <div onClick={closeModal} className="close-alert"><img src={IconcloseModal} alt="Logo cerrar modal" /></div>
            </div>
        </div>
    )
}
export default Modal;

you can use the setTimout function for this.您可以为此使用 setTimout function。 Assuming that closeModal is a function you can implement it like this.假设closeModal是 function 你可以像这样实现它。 This will close the modal after 3 seconds.这将在 3 秒后关闭模式。

function close() {
  setTimeout(() => closeModal(), 3000)
}
return(
...
<div onClick={close} className="close-alert"><img src={IconcloseModal} alt="Logo cerrar modal" /></div>
...
)

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

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