[英]ReactJs Element type is invalid Check the render method
I've been trying to solve the error when using tabs in my ReactJS application. 我一直在尝试在ReactJS应用程序中使用选项卡时解决错误。 So I've created a brand-new app with the command:
因此,我使用以下命令创建了一个全新的应用程序:
create-react-app my-app
and added the following to the App.js render() function as per react-bootstrap docs: 并根据react-bootstrap文档将以下内容添加到App.js render()函数中:
<Tabs defaultActiveKey={2} id="uncontrolled-tab-example">
<Tab eventKey={1} title="Tab 1">
Tab 1 content
</Tab>
<Tab eventKey={2} title="Tab 2">
Tab 2 content
</Tab>
<Tab eventKey={3} title="Tab 3" disabled>
Tab 3 content
</Tab>
</Tabs>
This is the resulting code: 这是结果代码:
import React, { Component, Tabs, Tab } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<Tabs defaultActiveKey={2} id="uncontrolled-tab-example">
<Tab eventKey={1} title="Tab 1">
Tab 1 content
</Tab>
<Tab eventKey={2} title="Tab 2">
Tab 2 content
</Tab>
<Tab eventKey={3} title="Tab 3" disabled>
Tab 3 content
</Tab>
</Tabs>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
The App.js file is imported in index.js. App.js文件导入到index.js中。 Same folder (src folder).
同一文件夹(src文件夹)。 This is the file content:
这是文件内容:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
I still get the 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
App
.检查
App
的渲染方法。
I'd say there is no problem with the export as the app works well without the Tabs snippet of code. 我想说,导出没有问题,因为没有Tabs代码段的应用程序运行良好。 Apparently, using tabs causes this problem, but I can't figure out what's wrong with the above.
显然,使用制表符会导致此问题,但我无法弄清楚上面的问题。
I will appreciate your help. 感谢您的帮助。 Many thanks.
非常感谢。
The error shows up because the module 'react'
doesn't actually export any component named Tabs
or Tab
, so when you try to import them from there, you'll get undefined
. 出现该错误是因为模块
'react'
实际上没有导出任何名为Tabs
或Tab
组件,因此当您尝试从那里导入它们时,将得到undefined
。
If the component comes from 'react-bootstrap'
, import it from there, not from 'react'
. 如果组件来自
'react-bootstrap'
,请从那里导入,而不是从'react'
导入。 See the Getting Started docs for an example. 有关示例,请参阅入门文档。
import React, { Component } from 'react'
import { Tabs, Tab } from 'react-bootstrap'
import logo from './logo.svg'
import './App.css'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.