简体   繁体   English

即使在 ReactJS 中导出组件后,默认导出错误

[英]Default export error even after exporting the component in ReactJS

I am trying to create a navigation bar using react-bootstrap by referring to their documentation .我正在尝试通过参考他们的文档来使用 react-bootstrap 创建一个导航栏。

I created a NavigationBar.js component file which I'll import.我创建了一个我将导入的 NavigationBar.js 组件文件。

import React from 'react';
import Navbar from 'react-bootstrap/Navbar';
import PropTypes from 'prop-types';

const NavigationBar = props => {
  const { link, brand } = props;
  return (
    <Navbar bg="light" sticky="top">
      <Navbar.brand href={link}>{brand}</Navbar.brand>
    </Navbar>
  );
};

NavigationBar.propTypes = {
  brand: PropTypes.string.isRequired,
  link: PropTypes.string.isRequired,
};

export default NavigationBar;

And my App.js (where I am importing the component):还有我的 App.js(我在其中导入组件):

import React from 'react';
import './App.css';

import NavigationBar from './components/NavigationBar';

function App() {
  return (
    <div>
      <NavigationBar brand="xyz" link="#home" />
    </div>
  );
}

export default App;

The code compiles fine but throws an error in the browser as follows:代码编译正常,但在浏览器中抛出错误,如下所示:

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.

Check the render method of `NavigationBar`.

What can be done to resolve this error?可以做些什么来解决这个错误?

I believe the error comes from this line我相信错误来自这条线

      <Navbar.brand href={link}>{brand}</Navbar.brand>

The names of the components in react have to start with capital letter. react中的组件名称必须以大写字母开头。 This means that the Navbar.brand should be Navbar.Brand这意味着 Navbar.brand 应该是 Navbar.Brand

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

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