简体   繁体   English

构建导航栏,我收到此错误 Element type is invalid: expected a string (for built-in components) or a class/function for complex components

[英]Building navbar and I get this error Element type is invalid: expected a string (for built-in components) or a class/function for composite components

I'm building a navbar for a project and the above message is telling me to check the render method in my header component.我正在为一个项目构建一个导航栏,上面的消息告诉我检查我的 header 组件中的渲染方法。 I've tried everything I can think of, including looking through answers on here.我已经尝试了我能想到的一切,包括在这里查看答案。 Specifically, I tried moving the Header component between my App component and the index.js file Not sure if I need a break or something but in any case, here's my header:具体来说,我尝试在我的 App 组件和 index.js 文件之间移动 Header 组件不确定我是否需要休息或其他什么,但无论如何,这是我的 header:

import React from "react";
import Nav from "react-bootstrap/Nav";
import Navbar from "react-bootstrap/Navbar";
import "bootstrap/dist/css/bootstrap.min.css";
import LinkContainer from "react-router-bootstrap";

const header = () => {
  return (
    <Navbar collapseOnSelect expand="lg">
      <LinkContainer to="/App">
      <Navbar.Brand href="/App">
        <img
          src="/PPLogoEdit.jpg"
          width="110"
          height="50"
          className="d-inline-block align-top"
          alt="pikelogo"
        />
      </Navbar.Brand>
      </LinkContainer>
      <Navbar.Toggle aria-controls="responsive-navbar-nav" />
      <Navbar.Collapse id="responsive-navbar-nav">
        <Nav className="mr-auto">
         <LinkContainer to="/App">
          <Nav.Link style={{ color: "yellow" }} href="/App">
            Home
          </Nav.Link>
          </LinkContainer>
          <LinkContainer to="/About">
          <Nav.Link style={{ color: "orange" }} href="/About">
            About
          </Nav.Link>
          </LinkContainer>
          <LinkContainer to="/Gallery">
          <Nav.Link style={{ color: "green" }} href="/Gallery">
            Gallery
          </Nav.Link>
          </LinkContainer>
          <LinkContainer to="/Shop">
          <Nav.Link style={{ color: "blue" }} href="/Shop">
            Shop
          </Nav.Link>
          </LinkContainer>
          <LinkContainer to="Contact">
          <Nav.Link style={{ color: "purple" }} href="/Contact">
            Contact
          </Nav.Link>
        </LinkContainer>
        </Nav>
      </Navbar.Collapse>
    </Navbar>
  );
}

export default header

and the index.js file:和 index.js 文件:

import React from "react";
import ReactDOM from "react-dom";
import 'bootstrap/dist/css/bootstrap.min.css'
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Header from './components/Header';
import App from "./components/App";
import About from "./components/About";
import Gallery from "./components/Gallery";
import Shop from "./components/Shop";
import Contact from "./components/Contact";

ReactDOM.render(<React.StrictMode>
  <Router>

 <Switch>
   <Route exact path="/" component={App} />
   <Route path="/about" component={About} />
   <Route path="/gallery" component={Gallery} />
   <Route path="/shop" component={Shop} />
   <Route path="/contact" component={Contact} />   
 </Switch>
</Router>
</React.StrictMode>, document.getElementById("root"));

Any help is appreciated, thank you!任何帮助表示赞赏,谢谢!

Look at the error messages:查看错误消息:

 Warning: React.createElement: 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. Check your code at Header.jsx:24. in Header (at src/index.js:15) in Router (created by BrowserRouter) in BrowserRouter (at src/index.js:14) in StrictMode (at src/index.js:13)

Repeats "Check your code at Header.jsx:XX" for all lines where LinkContainer is used.对使用 LinkContainer 的所有行重复“在LinkContainer :XX 检查您的代码”。

It's flagging the LinkContainer you try to import from react-router-bootstrap .它正在标记您尝试从react-router-bootstrap导入的LinkContainer You default import it but it should be a named import.您默认导入它,但它应该是命名导入。

import { LinkContainer } from "react-router-bootstrap";

编辑 building-navbar-and-i-get-this-error-element-type-is-invalid-expected-a-string

暂无
暂无

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

相关问题 错误 - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - error - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:ReactJS 中的对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 渲染错误元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Render Error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined NextJS:错误 - 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object - NextJS: error - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object 控制台错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Console error: Element type is invalid:expected a string (for built-in components) or a class/function (for composite components) but got: undefined React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)React JS - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) React JS 未捕获的错误:元素类型无效:预期为字符串(内置组件)或类/函数(复合组件),但得到:未定义 - Uncaught Error: Element type is invalid: expected a string (built-in components) or a class/function ( composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM