在React中使用Bootstrap中的Modal并使用Js控制(非使用Bootstrap-React)

在React中使用Bootstrap框架


组件Render

<button className='btn btn-primary btn-sm' onClick={() => openModal()}>详细</button>
<div className="modal fade" id="info-modal" tabIndex={-1} aria-labelledby="" aria-hidden="true">
 <div className="modal-dialog">
  <div className="modal-content">
   <div className="modal-header">
    <h1 className="modal-title fs-5" id="exampleModalLabel">通知详细</h1>
   </div>
   <div className="modal-body">
    ...
   </div>
   <div className="modal-footer">
    <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
   </div>
  </div>
 </div>
</div>

用于控制模态框的Js/Ts函数

const openModal = () => {
 const modal = new Modal('#info-modal');
 modal?.show();
}

2024年1月25日补充

在复用型模态框组件控制中,应使用Modal.getOrCreateInstance()代替new Modal(),原因是后者在多次调用(例如在模态框打开后试图使用hide()隐藏时)时会导致创建出与先前不同的实例,导致方法无法执行。

posted @   SILL  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示