我得到这个错误:

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;

当前回答

我不相信我的情况和补救措施在这里讨论,所以我补充。

我有一个函数,它返回一个名为notification的对象

import IconProgress from '../assets/svg/IconProgress';

  const getNotificationFromAttachment = (attachment) => {
    const notification = {
    Icon: IconProcessing,
    iconProps: {},
  };
  
  ...
  notification.Icon = {IconProgress};
  
  return notification
}

React期望一个字符串或类/函数作为变量通知的值。图标

所以你所需要做的就是将{IconProgress}改为IconProgress

见下图:

其他回答

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

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

这就是工作

当我在同一目录中有一个.jsx和.scss文件且根名称相同时,我遇到了这个错误。

举个例子,如果你有Component。jsx和Component。SCSS在同一个文件夹中,你试着这样做:

import Component from ./Component

Webpack显然混淆了,至少在我的情况下,当我真正想要.jsx文件时,它试图导入scss文件。

我可以通过重命名.scss文件并避免歧义来修复它。我还可以显式地导入Component.jsx

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

模板['无效字符串']

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

解决方案吗?

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

https://github.com/rackt/react-router/blob/e7c6f3d848e55dda11595447928e843d39bed0eb/examples/query-params/app.js#L4 路由器也是react-router的属性之一。 所以改变你的模块需要这样的代码:

  var reactRouter = require('react-router')
  var Router = reactRouter.Router
  var Route = reactRouter.Route
  var Link = reactRouter.Link

如果你想使用ES6语法的链接使用(导入),使用babel作为助手。

顺便说一句,为了让你的代码正常工作,我们可以添加{this.props。children}在App中, 就像

render() {
  return (
    <div>
      <h1>App</h1>
      <ul>
        <li><Link to="/about">About</Link></li>
      </ul>
      {this.props.children}
    </div>

  )
}