Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

how to open modal popup on app load using ionic react

i am using ionic-react and want to launch a modal popup when app is launched. The idea is to create a login popup when app starts. i will dismiss it if user is already logged in else user will login.

my current issue is how to open it automatically?

the way to open using button is known to me for code like below

 <IonModal isOpen={loginModal} cssClass='my-custom-class'>
        <p>This is modal content</p>
        <IonButton onClick={() => setLoginModal(false)}>Close Modal</IonButton>
      </IonModal>
      <IonButton onClick={() => setLoginModal(true)}>Show Modal</IonButton>

but i don't need button.

question from:https://stackoverflow.com/questions/66059495/how-to-open-modal-popup-on-app-load-using-ionic-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It opens automatically based on your isOpen which you have set to loginModal. When that is set to true, the modal will be opened. You should use a variable for that.

const [showModal, setShowModal] = useState(true);

...

return(
  <IonModal isOpen={showModal} cssClass='my-custom-class'>
  ...
  </IonModal>
)

edit: fixed typo brackets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...