为什么React Bootstrap Navbar会崩溃?

时间:2018-02-14 01:47:02

标签: javascript reactjs bootstrap-4

我按照React-Bootstrap文档,特别是这段代码来制作导航栏 - > https://react-bootstrap.github.io/components/navbar/#navbars-mobile-friendly

但是,我从谷歌浏览器清理了我的应用程序的会话,我的导航栏似乎保持崩溃。

enter image description here

我在互联网上搜索没有成功,因为我不确定问题是什么以及它来自哪里?

我的<Navbar/>代码

import React from 'react'
import { 
    Navbar as BoostrapNavBar,
    Nav,
    NavItem,
    MenuItem,
    NavDropdown } from 'react-bootstrap';
import { Link } from 'react-router-dom';

export default class Navbar extends React.Component {

    render () {
        return (
            <BoostrapNavBar inverse collapseOnSelect>
                <BoostrapNavBar.Header>
                    <BoostrapNavBar.Brand>
                        <a href="/">{this.props.navbar_title}</a>
                    </BoostrapNavBar.Brand>
                    <BoostrapNavBar.Toggle />
                </BoostrapNavBar.Header>
                <BoostrapNavBar.Collapse>
                    <Nav pullRight>
                    <NavItem componentClass={Link} href="/collections" to="/collections">Collections</NavItem>
                        <NavItem componentClass={Link} href="/bracelets" to="/bracelets">Bracelets</NavItem>
                        <NavItem componentClass={Link} href="/glasses" to="/glasses">Lunettes</NavItem>
                        <NavItem componentClass={Link} href="/watches" to="/watches">Montres</NavItem>
                        <NavDropdown eventKey={3} title="Support" id="basic-nav-dropdown"> 
                            <MenuItem componentClass={Link} href="/support" to="/support">Messages</MenuItem> 
                            <MenuItem componentClass={Link} href="/new_message" to="/new_message">Contacter le Support</MenuItem> 
                        </NavDropdown>
                        <NavItem componentClass={Link} href="/login" to="/login"><img src={'../resources/images/user.png'} alt="User"/></NavItem>
                        <NavItem componentClass={Link} href="/shopping_bag" to="/shopping_bag"><img src={'../resources/images/shopping-bag.png'} alt="Shopping-Bag"/></NavItem>
                    </Nav> 
                </BoostrapNavBar.Collapse>
            </BoostrapNavBar>
        );
    }
}

欢迎任何帮助!

基于评论:

<div id="root">
    <div>
        <nav class="navbar navbar-inverse">
            <div class="container">
                <div class="navbar-header"><a href="/" class="navbar-brand">ShopField</a><button type="button" class="navbar-toggle collapsed"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button></div>
                <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li role="presentation" class=""><a href="/collections">Collections</a></li>
                    <li role="presentation" class=""><a href="/bracelets">Bracelets</a></li>
                    <li role="presentation" class=""><a href="/glasses">Lunettes</a></li>
                    <li role="presentation" class=""><a href="/watches">Montres</a></li>
                    <li class="dropdown">
                        <a id="basic-nav-dropdown" role="button" class="dropdown-toggle" aria-haspopup="true" aria-expanded="false" href="#">Support <span class="caret"></span></a>
                        <ul role="menu" class="dropdown-menu" aria-labelledby="basic-nav-dropdown">
                            <li role="presentation" class=""><a href="/support" role="menuitem" tabindex="-1">Messages</a></li>
                            <li role="presentation" class=""><a href="/new_message" role="menuitem" tabindex="-1">Contacter le Support</a></li>
                        </ul>
                    </li>
                    <li role="presentation" class=""><a href="/login"><img src="../resources/images/user.png" alt="User"></a></li>
                    <li role="presentation" class=""><a href="/shopping_bag"><img src="../resources/images/shopping-bag.png" alt="Shopping-Bag"></a></li>
                </ul>
                </div>
            </div>
        </nav>
        <div>Home Page</div>
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

编辑:

目前,React Bootstrap仅适用于Bootstrap 3。

要解决您的问题,请按照我在此答案中概述的解决方案:

https://stackoverflow.com/a/48627104/8270343

一旦React Bootstrap与Bootstrap 4兼容,以下信息将变得相关:

由于您的导航栏中没有navbar-expand-*课程,因此一直处于崩溃状态。响应类告诉Bootstrap导航栏必须从哪个断点扩展。

因此,如果您希望它从lg断点开始扩展,即对大型(lg)和更大的屏幕进行扩展,则需要添加navbar-expand-lg您的导航栏的类。

这意味着:在您的情况下,需要调整您的反应代码,以便此类包含在导航栏的HTML输出中。

您可以在此处找到有关此课程的更多信息:

https://getbootstrap.com/docs/4.0/components/navbar/#responsive-behaviors

答案 1 :(得分:0)

以下是Bootstrap 4的工作示例。

先决条件:

npm install --save bootstrap@4.0.0
npm install --save reactstrap@next
npm install --save jquery@1.9.1
npm install --save react-transition-group
npm install --save react-popper
npm install react-router-bootstrap --save

确保安装了react-router-bootstrap。 LinkContainer是使链接可点击的组件。它必须放在DropdownItem之外才能在Firefox中运行。此外,将className =“collapse”添加到Collapse组件将最初在Firefox中隐藏菜单。

import React, { Component } from 'react';
import logo from '../img/logo_header.png';
import { LinkContainer } from 'react-router-bootstrap';
import { Button, ButtonGroup, NavDropdown, Collapse, Navbar, 
    NavbarToggler, NavbarBrand, Nav, NavItem, NavLink,
    Dropdown, DropdownMenu, DropdownToggle, DropdownItem, UncontrolledDropdown } from 'reactstrap';

class MyComponent extends Component{
    constructor(props) {
        super(props);

        this.toggleNavbar = this.toggleNavbar.bind(this);
        this.state = {
            isOpen: false
        };
    }

    toggleNavbar() {
        this.state.isOpen = App.myVar.isNavBarOpen; //fixes next time route change where isOpen=true
        let isOpen = !this.state.isOpen; //fixes initial delay in calling setState(), where clicking first time didn't open NavBar
        this.setState({
            isOpen: isOpen
        });
        App.myVar.isNavBarOpen = isOpen;
    }
    render(){
    return (
      <div>            
        <Navbar color="faded" light expand="md">
            <NavbarBrand href="/">
                <a href="http://example.com/" target="_blank"><img src={logo} alt="Logo" /></a>
                <a href="http://example.com/" target="_blank"><h2 className="header-title">My Site</h2></a>
            </NavbarBrand>                
            <NavbarToggler onClick={this.toggleNavbar} />
            <Collapse isOpen={App.myVar.isNavBarOpen} navbar className="collapse">
              <Nav className="ml-auto" navbar>
                <NavItem><LinkContainer to="/admin"><NavLink>Home</NavLink></LinkContainer></NavItem>
                <UncontrolledDropdown nav inNavbar>
                  <DropdownToggle nav caret>
                    Link 1
                  </DropdownToggle>
                  <DropdownMenu >
                    <LinkContainer to="/sub-link1">
                        <DropdownItem>Sub Link 1</DropdownItem>
                    </LinkContainer>
                  </DropdownMenu>
                </UncontrolledDropdown>                    
                <LinkContainer to="/logout">
                    <NavItem><NavLink>Logout</NavLink></NavItem>                                        
                </LinkContainer>
              </Nav>
            </Collapse>
        </Navbar>
      </div>
    )
    }
}

export default MyComponent;

如果我们点击一​​个菜单链接,就会发生错误,但路线会改变,但NavBar菜单会保持打开状态,因此,我们需要在父组件上使用全局变量,例如。 index.js来管理它的开放状态。

App.js

App.myVar = {
    isNavBarOpen: false
}

index.js

history.listen((location, action) => {    
    App.myVar.isNavBarOpen = false; //use global variable to manage NavBar isOpen in nested component
});

答案 2 :(得分:0)

此功能在react-bootstrap 1.3.0中正常工作。 它具有bootstrap 4.5。