Next.js + 引导程序 5 – 手动关闭模式

Next.js + bootstrap 5 – manually close the modal

提问人:Андрей Горев 提问时间:10/19/2023 最后编辑:Андрей Горев 更新时间:10/24/2023 访问量:58

问:

我在Next.js上的应用程序中使用bootstrap 5.2模式。我需要在成功返回请求后关闭模式窗口。 我使用属性 data-bs-toggle=“modal” 打开模态 使用文档和其他信息,我在组件中创建了一个函数:

 export function LoginModal() {
    const loginModalRef = useRef();

    const hideModal = () => {
        const { Modal } = require("bootstrap");
        const myModal = new Modal(loginModalRef.current);

        console.log(1,myModal,Modal)
        myModal.hide();
    };

   function sendRequest() {
        // if response success
        hideModal();
    }

    return (
        <>
            <div ref={loginModalRef} id="login-modal" className="modal fade">
                <div className="modal-dialog modal-dialog-centered">
                    <div className="modal-content">
                      <svg className="close-icon position-absolute pointer" data-bs-dismiss="modal">
                            <use xlinkHref="/images/sprite.svg#close-icon"></use>
                        </svg>
                        <div className="modal-body">
                            MyModalContetn
                            <div onClick={sendRequest}>Send</div>
                        </div>
                    </div>
                </div>
            </div>
        </>
    );
}

hide() 方法不起作用,模式窗口保持打开状态且没有错误

Bootstrap 已插入 _app.js

const App = ({ Component, pageProps }) => {
    // including bootstrap js
    useEffect(() => {
        require("bootstrap/dist/js/bootstrap.bundle.min.js");
    }, []);
/////

你能告诉我我做错了什么,以及它应该如何在我的案例中发挥作用吗?提前非常感谢

javascript reactjs next.js bootstrap-5

评论


答:

0赞 EdwinShdw 10/24/2023 #1

Bootstrap 文档中:

虽然 Bootstrap CSS 可以与任何框架一起使用,但 Bootstrap JavaScript 与 React、Vue 和 Angular 等 JavaScript 框架并不完全兼容 [...]。Bootstrap 和框架都可能尝试改变相同的 DOM 元素,从而导致卡在“打开”位置的下拉列表等错误。 对于那些使用这种类型的框架的人来说,更好的选择是使用特定于框架的包而不是 Bootstrap JavaScript。

在你的例子中,这个框架包将是 React Bootstrap