我得到这个错误:

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;

当前回答

我也遇到了同样的问题,并意识到我在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”,因此产生了未定义的结果:

模板['无效字符串']

其他回答

另一个可能的解决方案,对我来说很管用:

目前,react-router-redux处于测试阶段,npm返回4。X,但不是5.x。但是@types/react-router-redux返回5.x。这里使用了未定义的变量。

强制NPM/Yarn使用5。X帮我解出来了。

错误呢?这肯定是一个导入或导出的问题,你引用了一个组件或容器,要么你没有从它的定义脚本导出,要么你在导入它时使用了错误的格式。

解决方案吗?

遍历所有的组件/容器定义,并确保在脚本的末尾导出了它们,例如“export default 'yourcomponentname';”或者在组件定义的开头,例如“export default 'yourcomponentname'; Export const 'yourcomponentname'=() =>{ …… } 在导入语句中,确保没有根据相应的导出语句将命名导入和默认导入混为一谈

不要对一个问题的答案列表感到惊讶。造成这个问题的原因有很多;

对我来说,警告是

Warning .js:33警告:反应。createElement: type无效——期望一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:undefined。您可能忘记从定义组件的文件中导出组件。在index.js:13处检查代码。

后面是错误

元素类型无效:期望为字符串(对于内置组件)或类/函数(对于复合组件),但得到:undefined。您可能忘记从定义组件的文件中导出组件。

我无法理解这个错误,因为它没有提到任何方法或文件名。我只是在看了上面提到的这个警告后才解决了这个问题。

我在index.js中有如下一行。

<ConnectedRouter history={history}>

当我用关键字“ConnectedRouter”谷歌上面的错误时,我在GitHub页面上找到了解决方案。

这个错误是因为,当我们安装react-router-redux包时,默认情况下我们安装这个包。 https://github.com/reactjs/react-router-redux但不是实际的库。

要解决这个错误,请使用@指定npm作用域来安装正确的包

npm install react-router-redux@next

不需要删除错误安装的软件包。它将被自动覆盖。

谢谢你!

PS:即使是警告也能帮助你。不要只看错误而忽略警告。

在我的例子中,它是元标签。

我在用

const MetaTags = require('react-meta-tags');

我猜MetaTags是一个默认的导出,所以改变为这个解决了我,花了几个小时来弄清楚是哪一个导致它。

const { MetaTags } = require('react-meta-tags');

在我的例子中,这是由错误的注释符号引起的。这是错误的:

<Tag>
    /*{ oldComponent }*/
    { newComponent }
</Tag>

这是正确的:

<Tag>
    {/*{ oldComponent }*/}
    { newComponent }
</Tag>

注意花括号