简体   繁体   English

ReactJs元素类型无效检查render方法

[英]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'实际上没有导出任何名为TabsTab组件,因此当您尝试从那里导入它们时,将得到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.

相关问题 元素类型无效:检查渲染方法 - Element type is invalid: check the render method 元素类型无效:检查提供者的渲染方法(React Native) - Element type is invalid: Check render method of Providers (React Native) 错误:元素类型无效,检查相机的渲染方法 - Error: Element type is invalid with Check the render method of Camera React Native Element Type无效。 选中渲染方法 - React Native Element Type is Invalid. Check Render Method 组件异常:元素类型无效,请检查应用程序的渲染方法 - Component Exception: Element Type is Invalid, check render Method of app 检查`Drawer`的渲染方法。 元素类型无效错误 - Check the render method of `Drawer`. Element type is invalid Error 错误:元素类型无效,检查 React Native 中的渲染方法 - Error: Element type is invalid with Check the render method in React Native ReactJS - 元素类型无效 - ReactJS - Element type is invalid 元素类型无效:需要一个字符串但未定义,检查 AnimatedComponent 的渲染方法 - ReactNavigation - Element type is invalid: expected a string but got undefined, check the render method of AnimatedComponent - ReactNavigation 不变违规:元素类型无效:预期为字符串或类/函数,但获得了:对象。 检查的渲染方法 - Invariant Violation: Element type is invalid: expected a string or a class/function but got: object. Check the render method of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM