我得到这个错误:

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;

当前回答

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

目前,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

见下图:

在我的案例中(使用Webpack),这是以下两者的区别:

import {MyComponent} from '../components/xyz.js';

vs

import MyComponent from '../components/xyz.js';

第二个工作,而第一个造成错误。或者正好相反。

假设你的错误是:

未捕获错误:不变量违反:元素类型无效:期望为字符串(对于内置组件)或类/函数,但got: object

你有两个选择:

您的导出文件可以有字默认,如

export default class someNameHere

那么您的导入将需要避免在它周围使用{}。就像在

import somethingHere from someWhereHere

避免使用默认单词。然后你的导出是这样的

export class someNameHere

然后导入必须使用{}。就像

import {somethingHere} from someWhereHere

我在反应路由中得到了这个错误,问题是我正在使用

<Route path="/" component={<Home/>} exact /> .

但这是错误的路线需要组件作为类/函数,所以我把它改成了

<Route path="/" component={Home} exact />

这招奏效了。(只需避免在组件周围使用大括号)

我错误地试图从react-native而不是native-base导入容器和内容。

错误,因为这一行:

import { ActivityIndicator, RefreshControl,  StyleSheet, View, Keyboard, Container, Content } from 'react-native';

下面是代码修复:

import { ActivityIndicator, RefreshControl,  StyleSheet, View, Keyboard } from 'react-native';
import { Container, Content} from 'native-base';