使用Chrome与Ecmascript6类问题进行反应

时间:2017-01-20 17:23:16

标签: javascript reactjs ecmascript-6

我以React ES6作为编写React类的推荐方法。我从一个简单的例子开始:

import React from 'react';
import ReactDOM from 'react-dom';

require('../node_modules/font-awesome/css/font-awesome.css');
require('../node_modules/bootstrap/dist/css/bootstrap.css');

require('jquery');
require('bootstrap');

import Dashboard from './components/Dashboard/Dashboard';

ReactDOM.render(
  <Dashboard/>,
  document.getElementById('react-container')
);

我在ES6中的组件:

import React from 'react';


class Dashboard extends React.Component {
  render() {
    return <h1>Hello, Don Trump</h1>
  }
}

我在Chrome 55上遇到以下错误:

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in.
    at invariant (VM1093 bundle.js:9069)
    at ReactCompositeComponentWrapper.instantiateReactComponent [as _instantiateReactComponent] (VM1093 bundle.js:23166)
    at ReactCompositeComponentWrapper.performInitialMount (VM1093 bundle.js:23589)
    at ReactCompositeComponentWrapper.mountComponent (VM1093 bundle.js:23480)
    at Object.mountComponent (VM1093 bundle.js:16018)
    at mountComponentIntoNode (VM1093 bundle.js:28717)
    at ReactReconcileTransaction.perform (VM1093 bundle.js:17017)
    at batchedMountComponentIntoNode (VM1093 bundle.js:28739)
    at ReactDefaultBatchingStrategyTransaction.perform (VM1093 bundle.js:17017)
    at Object.batchedUpdates (VM1093 bundle.js:26233)

我认为有一些简单的东西我不见了。帮助appreacited。

3 个答案:

答案 0 :(得分:17)

错误消息可能正确:

  

您可能忘记从其定义的文件中导出组件。

导出您的UpdateOptions upsert = new UpdateOptions().upsert(true); Bson idFilter = Filters.eq("_id", oldDoc.getObjectId("_id")); //modify the oldDoc oldDoc. ... collection.replaceOne(idFilter, oldDoc, upsert); 组件,如下所示:

Dashboard

答案 1 :(得分:5)

添加

  

导出默认仪表板

在组件的最后; 所以新代码将是

class Dashboard extends React.Component {
  render() {
    return <h1>Hello, Don Trump</h1>
  }
}
export default Dashboard;

答案 2 :(得分:2)

您需要导出该课程。您实际上可以同时导出和声明

import React from 'react';

export default class Dashboard extends React.Component {
  render() {
    return <h1>Hello</h1>
  }
}