未捕获错误:元素类型无效:期望一个字符串(对于内置组件)

时间:2017-08-23 14:20:29

标签: reactjs

I am getting this error. I have defined all components in index.js and I am using react-dom.

Webpacker 本身正在编译,没有错误或警告。

错误:未捕获错误:元素类型无效:预期字符串(对于内置组件)或类/函数(对于复合组件)但得到:undefined。您可能忘记从其定义的文件中导出组件。 的 index.js

import 'core-js/fn/object/assign';
import React, { Component }  from 'react';
import ReactDOM from 'react-dom';
import { App } from './components/CustomerSearch';


// Render the main component into the dom
ReactDOM.render(<App />, document.getElementById('app'));

AppComponent.js

import React, { Component }  from 'react';
import {Grid} from 'Adding my own package here';
import EventEmitter from 'wolfy87-eventemitter';


export default class AppComponent extends React.Component {

  emitter = null;

  constructor() {
    super();

    this.emitter = new EventEmitter();

    this.emitter.addListener('result', function (args) {
    })

  }

  render() {
    return (
      <div className="Search">
        <Grid>
          <Grid.Column width={4}>
            <SearchForm updatechForm emitter={this.emitter}/>
          </Grid.Column>
          <Grid.Column width={12}>
            <ResultList emitter={this.emitter}/>
          </Grid.Column>
        </Grid>
      </div>
    );
  }
}

AppComponent.defaultProps = {};

错误

warning.js:35 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.
printWarning @ warning.js:35
warning @ warning.js:59
createElement @ ReactElementValidator.js:192
(anonymous) @ index.js:7
__webpack_require__ @ bootstrap 35891eb36bf51eb82cc9:19
module.exports @ bootstrap 35891eb36bf51eb82cc9:62
(anonymous) @ bootstrap 35891eb36bf51eb82cc9:62
invariant.js:44 Uncaught 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.
    at invariant (invariant.js:44)
    at ReactCompositeComponentWrapper.instantiateReactComponent [as _instantiateReactComponent] (instantiateReactComponent.js:74)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:366)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:257)
    at Object.mountComponent (ReactReconciler.js:45)
    at mountComponentIntoNode (ReactMount.js:104)
    at ReactReconcileTransaction.perform (Transaction.js:143)
    at batchedMountComponentIntoNode (ReactMount.js:126)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)

2 个答案:

答案 0 :(得分:2)

我正在解释错误,因为您可能忘记导出Grid(或者您的App)组件。那是你检查过的吗? (在定义这些组件的两个文件中)。

正如abdul所说,导入的格式可能是错误的。这取决于您是否已按exportexport default导出组件。

对于仅使用export导出的组件(或函数,常量等),您需要使用import {ComponentName} from ...的语法导入它。否则,如果它们是export default导出的,那么它们是在没有大括号(import ComponentName from ..)的情况下导入的。

答案 1 :(得分:1)

此错误很可能是由于您的import语句引起的。首先这是奇怪的import { App } from './components/CustomerSearch';,因为在这里我期望你有一个名为CustomSearch.js的文件,但是你的问题是它的AppComponent.js,所以我建议你改变它这样

import App from './components/AppComponent';

//此处,只要您正在进行您正在进行的默认导出,名称App并不重要。