简体   繁体   English

使用 react-bootstrap 时组件未定义

[英]Component is undefined when use react-bootstrap

I have code for react-bootstrap like this.我有这样的 react-bootstrap 代码。

import React from 'react'
import { Link } from 'react-router-dom'
import { Navbar, NavbarBrand, NavItem, Nav } from 'react-bootstrap'
import './App.css'
import NavbarToggle from 'react-bootstrap/esm/NavbarToggle'
import Routes from './Routes'
import NavbarCollapse from 'react-bootstrap/esm/NavbarCollapse'
import { LinkContainer } from 'react-router-bootstrap'

function App() {
  return (
    <div className="App container">
      <Navbar fluid collapseOnSelect>
        <Navbar.Header>
          <NavbarBrand>
            <Link to="/">Scratch</Link>
          </NavbarBrand>
          <NavbarToggle />
        </Navbar.Header>
        <NavbarCollapse>
          <Nav pullRight>
            <LinkContainer to="/signup">
              <NavItem>Signup</NavItem>
            </LinkContainer>
            <LinkContainer to="/login">
              <NavItem>Sign</NavItem>
            </LinkContainer>
          </Nav>
        </NavbarCollapse>
      </Navbar>
      <Routes />
    </div>
  )
}

export default App

it occurred errors.它发生错误。 The errors message are错误消息是

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

Note: when I omitted Navbar.Header it worked correctly.注意:当我省略Navbar.Header它工作正常。

NavBar.Header was removed in release v1 of React-bootstrap. NavBar.HeaderReact-bootstrap 的v1 版本中移除 Basically, if you add this component, it primarily adds to the DOM a div with a class of navbar-header .基本上,如果你添加这个组件,它主要是向 DOM 添加一个带有navbar-header类的div

<div class="navbar-header"></div>

If you are curious to know or want to replicate its behavior, you can take a look at this answer: What does "navbar-header" class do in Bootstrap?如果您很想知道或想复制它的行为,可以看看这个答案: Bootstrap 中的“navbar-header”类做了什么? or take a look at some outdated Bootstrap stylesheet (prior to v4).或查看一些过时的 Bootstrap 样式表(v4 之前)。

If you have the leisure of updating your layout, take a look at the updated examples of React-bootstrap Nav here: https://react-bootstrap.github.io/components/navbar/#navbars如果你有空闲更新你的布局,看看这里 React-bootstrap Nav 的更新示例: https : //react-bootstrap.github.io/components/navbar/#navbars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM