我得到这个错误:

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;

当前回答

就我而言,

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

其他回答

就我而言,

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

除了进口/出口问题提到。我发现使用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()文档。

对我来说,这是因为我的组件由于某种原因没有使用*符号导出,所以我必须显式地命名导出

// index.ts
export * from "./Button"

to

// index.ts
export { Button } from "./Button"

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

解决方案吗?

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

在我的例子中,我花了很多时间来搜索和检查可能的方法,但只有通过这种方式来修复:在导入自定义组件时删除“{”,“}”:

import OrderDetail from './orderDetail';

我的错误做法是:

import { OrderDetail } from './orderDetail';

因为在OrderDetail .js文件中,我将OrderDetail导出为默认类:

class OrderDetail extends Component {
  render() {
    return (
      <View>
        <Text>Here is an sample of Order Detail view</Text>
      </View>
    );
  }
}

export default OrderDetail;