我得到这个错误:

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帮我解出来了。

其他回答

除了进口/出口问题提到。我发现使用React.cloneElement()向子元素添加道具,然后渲染它给了我同样的错误。

我不得不改变:

render() {
  const ChildWithProps = React.cloneElement(
    this.props.children,
    { className: `${PREFIX}-anchor` }
  );

  return (
    <div>
      <ChildWithProps />
      ...
    </div>
  );
}

to:

render() {
  ...
  return (
    <div>
      <ChildWithProps.type {...ChildWithProps.props} />
    </div>
  );
}

有关更多信息,请参阅React.cloneElement()文档。

确保只返回一个PARENT - CHILD

这是可行的

<div>
    <p> Something here </p>
</div>

这行不通

<div>
    <p> Something here </p>
</div>
<div>
    <p> Something else here </p>
</div>

在文件底部导出可以解决这个问题。

const IndicatorCloak = (props) => { ... }
export default IndicatorCloak;

就我而言,

//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} 

在我的例子中,我只是在我的一个子模块的import- declaration中缺少了一个分号。

通过改变这个修正:

import Splash from './Splash'

:

import Splash from './Splash';