模态reactjs关闭时间

问题描述 投票:0回答:1

我需要一段时间后关闭我的模式,该怎么办?这是我到目前为止所做的

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;
javascript reactjs modal-dialog
1个回答
0
投票

您可以为此使用setTimout函数。假设closeModal是一个函数,您可以像这样实现它。这将在3秒钟后关闭模态。

function close() {
  setTimeout(() => closeModal(), 3000)
}
return(
...
<div onClick={close} className="close-alert"><img src={IconcloseModal} alt="Logo cerrar modal" /></div>
...
)
© www.soinside.com 2019 - 2024. All rights reserved.