Bootstrap 和导航栏折叠不工作React(create-react-app)

5m1hhzi4  于 2022-12-08  发布在  Bootstrap
关注(0)|答案(4)|浏览(157)

I'm trying to use: bootstrap 4 navbar and it wouldn't collapse on react. https://getbootstrap.com/docs/4.0/components/navbar/#supported-content
could someone explain why? I'm assuming this could be because will need to amend to components to change states? but it is just my guess.

import React from "react";
import ReactDOM from "react-dom";

import registerServiceWorker from "./registerServiceWorker";
// styles
import "../node_modules/jquery/dist/jquery.min.js";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";

ReactDOM.render(
<nav className="navbar navbar-expand-lg navbar-light bg-light">
  <a className="navbar-brand" href="#">Navbar</a>
  <button  className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span  className="navbar-toggler-icon"></span>
  </button>

  <div  className="collapse navbar-collapse" id="navbarSupportedContent">
    <ul  className="navbar-nav mr-auto">
      <li  className="nav-item active">
        <a  className="nav-link" href="#">Home <span  className="sr-only">(current)</span></a>
      </li>
      <li  className="nav-item">
        <a  className="nav-link" href="#">Link</a>
      </li>
      <li  className="nav-item dropdown">
        <a  className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div  className="dropdown-menu" aria-labelledby="navbarDropdown">
          <a  className="dropdown-item" href="#">Action</a>
          <a  className="dropdown-item" href="#">Another action</a>
          <div  className="dropdown-divider"></div>
          <a  className="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li  className="nav-item">
        <a  className="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form  className="form-inline my-2 my-lg-0">
      <input  className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"/>
      <button  className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>
, document.getElementById("root"));
registerServiceWorker();
ivqmmu1c

ivqmmu1c1#

导入jquery后,尝试在import中添加bootstrap.js

import "../node_modules/jquery/dist/jquery.min.js";
import "../node_modules/bootstrap/dist/js/bootstrap.min.js";
hmmo2u0o

hmmo2u0o2#

对我来说,最好的解决方案是在navbar组件中只导入bootstrap collapse.js脚本,以防止混合js脚本。
在navbar.jsx组件的顶部包含以下内容:
import "bootstrap/js/src/collapse.js";

owfi6suc

owfi6suc3#

如果是Bootstrap v3,则可以尝试将类navbar-toggler更改为navbar-toggle
在npm页面上,Bootstrap 3.3.7是最新版本。

368yc8dk

368yc8dk4#

如果使用react,请确保NavbarToggle和NavbarCollapse位于Navbar标签内

<Navbar expand="lg">
    <NavbarToggle aria-controls="basic-navbar-nav" />
    <NavbarOffcanvas>
        <Nav>
            <Nav.Item>
                <Nav.Link eventKey={TPL_TABS.ACCEPTED} href="/">Accepted</Nav.Link>
            </Nav.Item>
            <Nav.Item>
                <Nav.Link eventKey={TPL_TABS.ARCHIVE} href="/">Archive</Nav.Link>
            </Nav.Item>
        </Nav>
    </NavbarOffcanvas>
</Navbar>

相关问题