javascript - 即使在 ReactJS 中导出组件后,默认导出错误
问题描述
我正在尝试通过参考他们的文档来使用 react-bootstrap 创建一个导航栏。
我创建了一个我将导入的 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;
还有我的 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;
代码编译正常,但在浏览器中抛出错误,如下所示:
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`.
可以做些什么来解决这个错误?
解决方案
我相信错误来自这条线
<Navbar.brand href={link}>{brand}</Navbar.brand>
react中的组件名称必须以大写字母开头。这意味着 Navbar.brand 应该是 Navbar.Brand
推荐阅读
- ms-word - 通过 pandoc 添加 MS Word 书签
- c# - 单击asp链接按钮后保持gridview排序
- windows - python如何静音麦克风
- angular - 页面初始角度 4 上的默认数据
- ios - iOS CorePlot:y 轴呈现在错误的位置
- android - Android 横向活动首先以纵向打开,然后切换到横向
- jenkins - 用于针对构建进行编码标准验证的最佳 Jenkins 插件(对于 .NET)
- ruby-on-rails - IntegrationTest 从配置中引发错误?
- angular - Karma 的角度测试错误
- google-apps-script - 以域管理员身份从 Google 课堂检索课程作业资源时出现权限错误