Ala*_*lak 2 dom reactjs react-bootstrap virtual-dom bootstrap-5
我在 React 中使用 CDN 的 Bootstrap 5。我有一个组件将在反应组件之一中使用 Popover。因此,根据文档,它说“您必须在 bootstrap.js 之前包含 popper.min.js 或使用包含 Popper 的 bootstrap.bundle.min.js / bootstrap.bundle.js 才能使弹出窗口正常工作!”
并且要初始化页面上的所有弹出窗口,请使用给定的 javascript 代码:
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-
toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
})
Run Code Online (Sandbox Code Playgroud)
但我无法在我的反应组件中的 useEffect 中使用它,因为它返回“bootstrap.Popover(popoverTriggerEl)”对象,该对象在我的反应组件中未定义。
请问有人可以指导我吗?
现在Bootstrap 5 是模块化的,您可以导入组件,或直接从bootstrap...引用它们
var popover = new bootstrap.Popover(document.querySelector('#myButton'), options)
Run Code Online (Sandbox Code Playgroud)
所以要将它与 React useEffecthook 一起使用,你可以这样做......
function PopoverDemo() {
const popoverRef = useRef()
useEffect(() => {
var popover = new bootstrap.Popover(popoverRef.current, {
content: "Hello popover content!",
title: "My Popover"
})
})
return (
<div className="p-4">
<button className="btn btn-lg btn-danger" ref={popoverRef}>
Click to toggle popover
</button>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4371 次 |
| 最近记录: |