我得到这个错误:

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

这是我的代码:

var React = require('react')
var ReactDOM =  require('react-dom')
var Router = require('react-router')
var Route = Router.Route
var Link = Router.Link

var App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
        </ul>
      </div>
    )
  }
})

var About = require('./components/Home')
ReactDOM.render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
    </Route>
  </Router>
), document.body)

我的家。jsx文件:

var React = require('react');
var RaisedButton = require('material-ui/lib/raised-button');

var Home = React.createClass({
  render:function() {
    return (
        <RaisedButton label="Default" />
    );
  },
});

module.exports = Home;

当前回答

@Balasubramani M救了我。想再加一个来帮助人们。当你把太多的东西粘在一起,对版本漫不经心时,这就是问题所在。我更新了material-ui的一个版本,需要更改

import Card, {CardContent, CardMedia, CardActions } from "@material-ui/core/Card"; 

:

import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';

其他回答

React Native最新版本0.50及以上也有类似问题。

对我来说,这是一种区别:

import App from './app'

and

import App from './app/index.js'

(后者解决了这个问题)。我花了好几个小时才发现这个奇怪的,很难注意到的细微差别:(

作为一个快速的补充。我也遇到了同样的问题,当Webpack编译我的测试时,应用程序运行良好。当我将我的组件导入到测试文件中时,我在其中一个导入中使用了错误的大小写,这导致了同样的错误。

import myComponent from '../../src/components/myComponent'

应该是

import myComponent from '../../src/components/MyComponent'

注意,导入名称myComponent依赖于myComponent文件中导出的名称。

在我的例子中,导入是由于库而隐式发生的。

我设法通过改变来解决它

导出类Foo

to

导出默认类Foo。

我也犯了同样的错误: 错误修复!!!!

我使用'react-router-redux' v4,但她不好.. 在npm安装react-router-redux@next 我在"react-router-redux"上:"^5.0.0-alpha.9",

这就是工作

我也遇到了同样的问题,并意识到我在JSX标记中提供了一个未定义的React组件。问题是要渲染的react组件是动态计算的,它最终没有定义!

错误说明:

不变性违反:元素类型无效:期望为字符串(对于内置组件)或类/函数(对于复合组件),但得到:undefined。您可能忘记从定义组件的文件中导出组件。检查C.的渲染方法,

产生此错误的示例:

var componentA = require('./components/A'); var componentB = require('./components/B'); const templates = { a_type: componentA, b_type: componentB } class C extends React.Component { constructor(props) { super(props); } // ... render() { //Let´s say that depending on the uiType coming in a data field you end up rendering a component A or B (as part of C) const ComponentTemplate = templates[this.props.data.uiType]; return <ComponentTemplate></ComponentTemplate> } // ... }

问题是提供了一个无效的“uiType”,因此产生了未定义的结果:

模板['无效字符串']