【问题标题】:Adding Bootstrap Navbar to create-react-app project将 Bootstrap Navbar 添加到 create-react-app 项目
【发布时间】:2017-09-05 18:42:11
【问题描述】:

我有一个使用create-react-app 创建的全新项目。我添加了引导程序:

npm install --save bootstrap@3

我已经将它导入到我的根目录index.js

import 'bootstrap/dist/css/bootstrap.css';

现在,我已将 Bootstrap documentation 中的导航栏示例添加到默认 App.js jsx 的顶部,我得到了一个未格式化的混乱。

我知道 bootstrap 正在这个文件中工作,因为我还添加了一个通过 bootstrap 正确设置样式的按钮。但是导航栏根本不起作用。

这是我的App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <nav className="navbar navbar-toggleable-md navbar-light bg-faded">
          <button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span className="navbar-toggler-icon"></span>
          </button>
          <a className="navbar-brand" href="#">Navbar</a>
          <div className="collapse navbar-collapse" id="navbarNav">
            <ul className="navbar-nav">
              <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="#">Features</a>
              </li>
              <li className="nav-item">
                <a className="nav-link" href="#">Pricing</a>
              </li>
              <li className="nav-item">
                <a className="nav-link disabled" href="#">Disabled</a>
              </li>
            </ul>
          </div>
        </nav>
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
        <button className="btn">Button</button>
      </div>
    );
  }
}

export default App;

【问题讨论】:

  • 问题可能与您使用 v4 bootstrap 示例和 bootstrap v3 有关。
  • 如果您无法在 JSFiddle 中重现问题,那么很可能是您的设置中存在问题。检查导航栏时会发生什么? html甚至出现了吗? Create-react-app 使用服务工作者进行缓存,因此您可能需要重新启动服务器并进行硬刷新才能看到您的更改。
  • @bennygenel 这是正确答案。一旦我将我的 npm 包更新到最新版本(bootstrap@4.0.0-alpha.6),它就可以工作了。如果您想重申这一点作为答案,我会接受。

标签: javascript twitter-bootstrap reactjs


【解决方案1】:

问题是使用 Bootstrap v3 和 Bootstrap v4 示例。使用 v3 Navbar 或引导版本更新代码到 v4 应该可以解决问题。

【讨论】:

    【解决方案2】:

    不要以这种方式使用 Bootstrap。请改用react-bootstrap。这是一个链接:

    React Bootstrap

    示例代码:

    const NavDropdownExample = React.createClass({
      handleSelect(eventKey) {
        event.preventDefault();
        alert(`selected ${eventKey}`);
      },
    
      render() {
        return (
          <Nav bsStyle="tabs" activeKey="1" onSelect={this.handleSelect}>
            <NavItem eventKey="1" href="/home">NavItem 1 content</NavItem>
            <NavItem eventKey="2" title="Item">NavItem 2 content</NavItem>
            <NavItem eventKey="3" disabled>NavItem 3 content</NavItem>
            <NavDropdown eventKey="4" title="Dropdown" id="nav-dropdown">
              <MenuItem eventKey="4.1">Action</MenuItem>
              <MenuItem eventKey="4.2">Another action</MenuItem>
              <MenuItem eventKey="4.3">Something else here</MenuItem>
              <MenuItem divider />
              <MenuItem eventKey="4.4">Separated link</MenuItem>
            </NavDropdown>
          </Nav>
        );
      }
    });
    
    ReactDOM.render(<NavDropdownExample />, mountNode);
    

    【讨论】:

    • 我在create-react-app 指南中看到了这一点,但我想知道为什么这是首选。我发现为了可读性,坚持众所周知的约定,比如在 className 中使用 bootstrap 会更好。此外,当 bootstrap 更新时,我们无法确定 react-bootstrap 的创建者是否会保持最新状态。
    • 根据我的经验,使用 className 根本行不通。有趣的是,还有一些与折叠事物相关的 JS,例如菜单和显示模式。
    猜你喜欢
    • 2019-02-16
    • 2021-02-13
    • 2017-10-09
    • 1970-01-01
    • 2018-06-12
    • 2021-06-02
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多