我得到这个错误:

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组件模块化了?如果是,如果忘记指定模块,就会得到这个错误。出口,例如:

非模块化以前有效的组件/代码:

var YourReactComponent = React.createClass({
    render: function() { ...

使用module.exports模块化组件/代码:

module.exports = React.createClass({
    render: function() { ...

其他回答

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

import {Link} from 'react-router'

相反,使用它可能会更好

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

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

在我的情况下,我使用默认导出,但不导出函数或React。组件,但只是一个JSX元素,即。

错误:

export default (<div>Hello World!</div>)

工作原理:

export default () => (<div>Hello World!</div>)

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

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

我写错了:

import {Ionicons} from 'expo';

您需要导出default或require(path).default

var About = require('./components/Home').default

就我而言,

//details.js 
import React from 'react'

const Details = (props) =>{
return <h1>Details</h1>
}

export default Details;


//main.js
import React from "react";
import { withRouter } from "react-router";
import {Route,BrowserRouter as Router,Redirect,Switch,Link} from "react-router-dom";
import Details from "./myAccount/details";

const Main = (props) => {
 return (
   <>
        <Switch>
          <Route path="/my-account/details" component={<Details />} />
        </Switch>
   </>

 );
};

export default withRouter(Main);

===========================

Replaced with below one, error got fixed
component={<Details />}  with  component={Details}