【问题标题】:Bootstrap based Modal is not appearing in React / Next JS application基于 Bootstrap 的 Modal 没有出现在 React / Next JS 应用程序中
【发布时间】:2020-06-07 19:54:35
【问题描述】:

一段时间以来,我一直在尝试向我的网站添加模式,但无论我尝试了多少,我遵循的示例,它都没有显示出来。我正在将 React 与 Next JS 一起使用

我试过了 this and this and this 但由于某种原因,我无法让模态出现在屏幕上。 此外,我删除并重新安装了我的 node_modules 文件夹和 yarn.lock ,但这也没有什么不同。 这是我的步骤:

  1. https://getbootstrap.com/docs/4.4/components/modal/ 的引导组件中获取示例模式代码
  2. 在 src/components 中创建了一个 Modal.js 文件
  3. 在 index.js 中引用。

结果:我看到“启动演示模式”按钮,但是当我单击时,什么也没有发生。没有错误,什么都没有。我在谷歌浏览器和野生动物园都试过了,结果是一样的。

这是我的 Modal.js 组件:

import React from "react";

const Modal = () => {
  console.log("is this being called?");
  return (
    <div>
      <button
        type="button"
        className="btn btn-primary"
        data-toggle="modal"
        data-target="#exampleModal"
      >
        Launch demo modal
      </button>

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

export default Modal;

我如何在 index.js 中调用它

import Modal from "../src/components/Modal";

const Index = ({ posts }) => {

 return (
    <Layout>
      <Navigation />
      <head>
      ....
      <Modal />
.....
)

index.html 中引用的脚本

   <script src="js/jquery/jquery.min.js"></script>
    <script src="js/popper/popper.min.js"></script>
    <!-- <script src="js/bootstrap/bootstrap.min.js"></script> -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />

和 Package.json

{
  "name": "testapp",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "12.2.0",
    "npm": "6.4.1",
    "yarn": "1.13.0"
  },
  "dependencies": {
    "@sendgrid/contact-importer": "^6.5.1",
    "@sendgrid/mail": "^6.5.1",
    "@zeit/next-css": "^1.0.1",
    "@zeit/next-sass": "^1.0.1",
    "body-parser": "^1.19.0",
    "bootstrap": "^4.4.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "isomorphic-unfetch": "^3.0.0",
    "next": "^9.1.4",
    "next-images": "^1.2.0",
    "node-sass": "^4.13.1",
    "raw-loader": "^4.0.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-ga": "^2.7.0",
    "react-markdown": "^4.2.2",
    "react-scripts": "3.2.0"
  },
  "scripts": {
    "dev": "node server/index",
    "build": "next build",
    "start": "next start -p $PORT"
  },
  "eslintConfig": {
    "extends": "react-app"
  }
}

感谢您的帮助。

附言。 Bootstrap 样式确实有效,但模态无效。例如: &lt;div className="col-md-6 my-auto"&gt; works just fine.fors 就好了。

编辑 1: 我包含到 index.html

        <script src="js/jquery/jquery.min.js"></script>
        <script src="js/popper/popper.min.js"></script>
        <script src="js/bootstrap/bootstrap.min.js"></script>

在 index.js 中

import $ from 'jquery'; 
                <button
                  type="button"
                  className="btn btn-primary"
                  onClick={() => $("#exampleModal").modal("show")}
                >
                  <Modal />
                </button>

然后我收到以下错误:

编辑 2:

从 index.js 中删除 jquery 导入并在开发者控制台中运行 $("#exampleModal").modal("show") 我得到 modal is not a function error now

编辑 3: 所以我添加了“reactstrap”并使用了这里提到的一种模式:https://reactstrap.github.io/components/modals/ 然后在我的索引文件中,我将其添加为按钮的一部分,如下所示:

   <button
    className="btn btn-primary"
    onClick={showConfirmationResult}
    >
    Submit
    <ModalExample isOpen={false} toggle={true} />
    </button>

然后它才起作用,但这并不理想。我想根据按钮点击触发模态,所以我尝试了这个:

 const showConfirmationResult = () => {
    return <ModalExample isOpen={false} toggle={true} />;
  };

但这不起作用,我不知道为什么。

感谢您的帮助。

【问题讨论】:

标签: javascript reactjs next.js


【解决方案1】:

基于data-targetdata-toggle 的触发模态的方法将不起作用,因为您的dom 是由react 动态创建的。这意味着您的引导 javascript 甚至在该按钮由 react 呈现之前就已完成运行。因此,您应该使用 javascript 方法触发它 https://getbootstrap.com/docs/4.4/components/modal/#via-javascript

把你的按钮改成这样。

  <button
    type="button"
    className="btn btn-primary"
    onClick={()=> $('#exampleModal').modal('show')}
  >
    Launch demo modal
  </button>

【讨论】:

  • 嗨@ssk,我尝试了你的建议,但似乎我错过了一些东西。我最初收到“$ 未定义”的静态编译器错误,然后我将 jquery 添加到代码库中。作为第二个错误,我收到此错误:“TypeError: jquery__WEBPACK_IMPORTED_MODULE_9___default(...)(...).modal is not a function onClick” 关于为什么会发生这种情况的任何想法?
  • 我看到你已经在 index.html 中包含了 jquery 和 bootstrap。你确定你已经按照引导框架的要求包含了 jquery bootstrap 和 popper。
  • 如果你已经在脚本标签中包含了 jquery,你不必import $ from 'jquery';。避免这种情况并在开发者工具控制台中尝试$("#exampleModal").modal("show")
  • @Maniraj Murugan 我尝试了reactstrap 并取得了半成功,但仍然无法正常工作
猜你喜欢
  • 1970-01-01
  • 2021-01-12
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
相关资源
最近更新 更多