Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ReactJS show modal after delay

I’m trying to show a Bootstrap modal after delay, however only home page is shown. Please see my code below (no error is detected).

App.js:

import { useEffect, useState } from "react";
import Home from "./pages/Home";
import Modal from 'react-bootstrap/Modal';

function MyVerticallyCenteredModal() {
    return (
      <Modal
        size="lg"
        aria-labelledby="contained-modal-title-vcenter"
        centered
      >
        <Modal.Header closeButton>
          <Modal.Title id="contained-modal-title-vcenter">
            Modal heading
          </Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <h4>Centered Modal</h4>
          <p>
            Some Text
          </p>
        </Modal.Body>
      </Modal>
    );
}

function App() {

    const [modalShow, setModalShow] = useState(false);

    useEffect(() => {
        const timeId = setTimeout(() => setModalShow(true), 3000)
    
        return () => clearTimeout(timeId)
    })

    return (
        <div className="App">
            <Home />
            {modalShow && (<MyVerticallyCenteredModal />)}
        </div>
    );
}

export default App;

Do you have any idea what’s wrong? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t think the timeout logic is the problem here, because even if the conditional modalShow is removed, the component still doesn’t render.

Try <Modal.Dialog> instead of just <Modal> for the bootstrap component

https://react-bootstrap.github.io/components/modal/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading