我得到这个错误:

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;

当前回答

我也遇到了同样的问题,因为我确实导入了不正确的库,所以我检查了库中的文档,路由用新的versión更改了,解决方案是这样的:

import {Ionicons} from '@expo/vector-icons';

我写错了:

import {Ionicons} from 'expo';

其他回答

如果你得到这个错误,这可能是因为你导入链接使用

import {Link} from 'react-router'

相反,使用它可能会更好

import { Link } from 'react-router-dom'
                      ^--------------^

我相信这是react路由器版本4的一个要求

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

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

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

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

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

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

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

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

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

见下图:

这意味着您导入的某些组件声明错误或不存在

我也有类似的问题,真的

import { Image } from './Shared'

但是当我查看共享文件时,我没有'Image'组件,而是'ItemImage'组件

import { ItemImage } from './Shared';

当你从其他项目复制代码时会发生这种情况;)

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

模板['无效字符串']