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();
4条答案
按热度按时间ivqmmu1c1#
导入
jquery
后,尝试在import
中添加bootstrap.js
:hmmo2u0o2#
对我来说,最好的解决方案是在navbar组件中只导入bootstrap collapse.js脚本,以防止混合js脚本。
在navbar.jsx组件的顶部包含以下内容:
import "bootstrap/js/src/collapse.js";
owfi6suc3#
如果是Bootstrap v3,则可以尝试将类
navbar-toggler
更改为navbar-toggle
。在npm页面上,Bootstrap 3.3.7是最新版本。
368yc8dk4#
如果使用react,请确保NavbarToggle和NavbarCollapse位于Navbar标签内