我得到这个错误:

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上遇到了点问题。Fragment,因为我的react的版本是< 16.2,所以我去掉了它。

其他回答

你是否已经将React组件模块化了?如果是,如果忘记指定模块,就会得到这个错误。出口,例如:

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

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

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

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

在我的例子中,它最终是像这样导入的外部组件:

import React, {Component}

然后声明如下:

导出默认类MyOuterComponent

其中一个内部组件导入了React bare:

import React from

用虚线表示声明:

导出默认类MyInnerComponent

就我而言,

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

https://github.com/rackt/react-router/blob/e7c6f3d848e55dda11595447928e843d39bed0eb/examples/query-params/app.js#L4 路由器也是react-router的属性之一。 所以改变你的模块需要这样的代码:

  var reactRouter = require('react-router')
  var Router = reactRouter.Router
  var Route = reactRouter.Route
  var Link = reactRouter.Link

如果你想使用ES6语法的链接使用(导入),使用babel作为助手。

顺便说一句,为了让你的代码正常工作,我们可以添加{this.props。children}在App中, 就像

render() {
  return (
    <div>
      <h1>App</h1>
      <ul>
        <li><Link to="/about">About</Link></li>
      </ul>
      {this.props.children}
    </div>

  )
}

@Balasubramani M救了我。想再加一个来帮助人们。当你把太多的东西粘在一起,对版本漫不经心时,这就是问题所在。我更新了material-ui的一个版本,需要更改

import Card, {CardContent, CardMedia, CardActions } from "@material-ui/core/Card"; 

:

import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';