如何通过在Reaction中单击按钮打开Bootstrap modal [英] How to open Bootstrap Modal from a button click in React

查看:12
本文介绍了如何通过在Reaction中单击按钮打开Bootstrap modal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Reaction和Bootstrap非常陌生。我正在尝试实现一个点击按钮的模式对话框。我的模式当前未显示-您可以在第二段代码中看到我如何尝试使用布尔show变量包括该模式。

我见过各种帮助完成此操作的库,但我想先尝试了解如何在没有这些库的情况下工作。

我不确定为什么未显示此模式。

const ModalContent = () => {
    return (
        <div className="modal">
          <div className="modal-dialog">
            <div className="modal-content">
              <div className="modal-header">
                <h5 className="modal-title">Modal title</h5>
                <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
              <div className="modal-body">
                <p>Modal body text goes here. {modalInfo}</p>
              </div>
              <div className="modal-footer">
                <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" className="btn btn-primary">Save changes</button>
              </div>
            </div>
          </div>
        </div>
    );
}

        <div className="row">
            <div className="col">
                <table className="table table-hover">
                    ...
                </table>

                <Pagination itemsCount={filtered.length}
                            pageSize={pageSize}
                            currentPage={currentPage}
                            onPageChange={handlePageChange}/>

            </div>
            {show ? <ModalContent /> : null}
        </div>

推荐答案

查看文档时,您缺少切换模式的按钮。

页面:https://getbootstrap.com/docs/4.0/components/modal/

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

您还可以使用以下代码行设置自定义触发器:

使用一行脚本调用id为mymodal的模型:

$('#myModal').modal(options)

选项:https://getbootstrap.com/docs/4.0/components/modal/#options

(未测试)您还可以尝试使用Document.getElementById()获取元素并调用模式。

这就是说,我不知道在您已经使用Reaction的情况下使用jQuery是不是一个好主意,但是我想您已经知道Reaction Bootstrap已经有一个实现了。

这篇关于如何通过在Reaction中单击按钮打开Bootstrap modal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆