gpt4 book ai didi

javascript - 单击按钮后,Bootstrap 5 模式未在 React.js 中显示

转载 作者:行者123 更新时间:2023-12-04 22:44:31 25 4
gpt4 key购买 nike

我正在使用 bootstrap5 进行 react 。我在 React 中成功安装了 bootstrap5。现在我必须在我的页面上显示模态,所以我在我的页面上添加了一个按钮和模态代码。
下面是我用于模态的代码。现在我点击按钮然后没有任何工作。我没有得到我的模态。
这里有什么问题?

import React from "react";

function addEmployee(){
return(
<div className="addEmployee">
<button type="button" className="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">Add Employee</button>

<div className="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body">
...
</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">Understood</button>
</div>
</div>
</div>
</div>


</div>

)
}
export default addEmployee;

最佳答案

Bootstrap 5 不再依赖于 jQuery 所以你不需要像 react-bootstrap 这样的第三方库在 React 中使用 Bootstrap。例如,您可以使用 data-bs-标记中的属性,使用显示/隐藏模态的方法为 Bootstrap 模态创建一个功能组件...

function ModalExample() {
const modalRef = useRef()

const showModal = () => {
const modalEle = modalRef.current
const bsModal = new bootstrap.Modal(modalEle, {
backdrop: 'static',
keyboard: false
})
bsModal.show()
}

const hideModal = () => {
const modalEle = modalRef.current
const bsModal= bootstrap.Modal.getInstance(modalEle)
bsModal.hide()
}

return(
<div className="addEmployee">
<button type="button" className="btn btn-primary" onClick={showModal}>Add Employee</button>
<div className="modal fade" ref={modalRef} tabIndex="-1" >
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" className="btn-close" onClick={hideModal} aria-label="Close"></button>
</div>
<div className="modal-body">
...
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" onClick={hideModal}>Close</button>
<button type="button" className="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
</div>
)
}
Demo of both methods

关于javascript - 单击按钮后,Bootstrap 5 模式未在 React.js 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65444951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com