将Bootstrap Navbar添加到create-react-app项目

时间:2017-09-05 18:42:12

标签: javascript twitter-bootstrap reactjs

我有一个用create-react-app创建的全新项目。我添加了bootstrap:

npm install --save bootstrap@3

我已将其导入我的根index.js

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

现在我已经将Bootstrap documentation中的Navbar示例添加到默认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;

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

不要那样使用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);