使用react.js显示bootstrap navbar

时间:2017-03-29 19:51:47

标签: javascript twitter-bootstrap reactjs

我有一个非常简单的React.js项目,我试图在项目中添加一个bootstrap导航栏,但我似乎无法弄清楚如何让导航栏显示在页面上。我可以在此处找到我引用的导航栏示例http://codepen.io/zhaozhiming/pen/LNGyvR

该项目如下所示,

的index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <title>My super cool title.</title>
</head>
<body>
  <div id="root"></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="bundle.js" ></script>
</body>
</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.js';

ReactDOM.render(<App />, document.getElementById('root'));

App.js

import React, {PropTypes} from 'react';
import styles from './App.css';
import NavBarTest from './common/NavBar-test';

const App = () => {
  return (
    <div id="parent">
      <div>Hello, App!</div>
      <NavBarTest {...NavBarTest} />
    </div>
  )
}

export default App;

Navbar的test.js

import React from 'react';

// create classes
var NavBar = React.createClass({
  render: function(){
    return(
      <nav className="navbar navbar-inverse">
        <div className="container-fluid">
          <div className="navbar-header">
            <button type="button" className="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
              <span className="sr-only">Toggle navigation</span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
            </button>
            <NavBrand linkTo={this.props.brand.linkTo} text={this.props.brand.text} />
          </div>
          <div className="collapse navbar-collapse" id="navbar-collapse">
            <NavMenu links={this.props.links} />
          </div>
        </div>
      </nav>
    );
  }
});

var NavBrand = React.createClass({
  render: function(){
    return (
      <a className="navbar-brand" href={this.props.linkTo}>{this.props.text}</a>
    );
  }
});

var NavMenu = React.createClass({
  render: function(){
    var links = this.props.links.map(function(link){
      if(link.dropdown) {
        return (
          <NavLinkDropdown links={link.links} text={link.text} active={link.active} />
        );
      }
      else {
        return (
          <NavLink linkTo={link.linkTo} text={link.text} active={link.active} />
        );
      }
    });
    return (
      <ul className="nav navbar-nav">
        {links}
      </ul>
    );
  }
});

var NavLinkDropdown = React.createClass({
  render: function(){
    var active = false;
    var links = this.props.links.map(function(link){
      if(link.active){
        active = true;
      }
      return (
        <NavLink linkTo={link.linkTo} text={link.text} active={link.active} />
      );
    });
    return (
      <li className={"dropdown " + (active ? "active" : "")}>
        <a href="#" className="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
          {this.props.text}
          <span className="caret"></span>
        </a>
        <ul className="dropdown-menu">
          {links}
        </ul>
      </li>
    );
  }
});

var NavLink = React.createClass({
  render: function(){
    return(
      <li className={(this.props.active ? "active" : "")}><a href={this.props.linkTo}>{this.props.text}</a></li>
    );
  }
});

// set data
var navbar = {};
navbar.brand =
  {linkTo: "#", text: "React Bootstrap Navbar"};
navbar.links = [
  {linkTo: "#", text: "Link 1"},
  {linkTo: "#", text: "Link 2"},
  {dropdown: true, text: "Dropdown", links: [
    {linkTo: "#", text: "Dropdown Link 1"},
    {linkTo: "#", text: "Dropdown Link 2", active: true}
  ]}
];

export default NavBar;

最后,页面似乎呈现,但我在浏览器控制台中看到以下错误/警告消息。关于如何让这个导航栏显示在页面上的任何帮助将非常感激。

error message

2 个答案:

答案 0 :(得分:3)

根据您显示的结构,您应该在NavBar组件中呈现App,因为您是从NavBar文件默认导出Navbar-test.js。在NavBar组件中,您正在按this.props.brand访问值,因此您需要定义var navbar数据(您在app.js文件的最后一个文件中定义的数据< / {>)在app.js文件中,在app.js文件中使用它,它将起作用:

import React, {PropTypes} from 'react';
import styles from './App.css';
import NavBar from './common/NavBar-test';

const App = () => {
  return (
    <div id="parent">
      <div>Hello, App!</div>
      <NavBar {...navbar}/>
    </div>
  )
}

var navbar = {};
navbar.brand =  {
                    linkTo: "#", 
                    text: "React Bootstrap Navbar"
                };
navbar.links = [
        {
            linkTo: "#", 
            text: "Link 1"
        },
        {
            linkTo: "#", 
            text: "Link 2"
        },
        {
            dropdown: true, 
            text: "Dropdown", 
            links: [
                {
                    linkTo: "#", 
                    text: "Dropdown Link 1"
                },
                {
                    linkTo: "#", 
                    text: "Dropdown Link 2", 
                    active: true
                }
            ]
        }
];

检查工作代码:http://codepen.io/zhaozhiming/pen/LNGyvR?editors=0010

答案 1 :(得分:1)

如果这是为了学习,那么请继续创建它们!否则我建议使用react-bootstrap

https://react-bootstrap.github.io/components/navbar/