本文翻译自: Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

I am getting this error: 我收到此错误:

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. 未捕获的错误:始终违反:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。

This is my code: 这是我的代码:

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 (
        <h1>App</h1>
          <li><Link to="/about">About</Link></li>
var About = require('./components/Home')
ReactDOM.render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
    </Route>
  </Router>
), document.body)

My Home.jsx file: 我的Home.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;

参考:https://stackoom.com/question/2JCuZ/未捕获的错误-始终违反-元素类型无效-预期为字符串-对于内置组件-或类-函数-但得到了-对象

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

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

https://github.com/rackt/react-router/blob/e7c6f3d848e55dda11595447928e843d39bed0eb/examples/query-params/app.js#L4 Router is also one of the properties of react-router . https://github.com/rackt/react-router/blob/e7c6f3d848e55dda11595447928e843d39bed0eb/examples/query-params/app.js#L4 Router也是react-router的属性之一。 So change your modules require code like that: 因此,更改模块需要这样的代码:

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

If you want to use ES6 syntax the link use( import ), use babel as helper. 如果要使用ES6语法链接use( import ),请使用babel作为帮助程序。

BTW, to make your code works, we can add {this.props.children} in the App , like 顺便说一句,为了使您的代码正常工作,我们可以在App添加{this.props.children} ,例如

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

Have you just modularized any of your React components? 您是否刚刚对任何React组件进行了模块化? If yes, you will get this error if you forgot to specify module.exports , for example: 如果是,则如果忘记指定module.exports ,则会出现此错误,例如:

non-modularized previously valid component/code: 非模块化先前有效的组件/代码:

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

modularized component/code with module.exports : 带有module.exports的模块化组件/代码:

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

In my case ( using Webpack ) it was the difference between: 就我而言( 使用Webpack )是以下两者之间的区别:

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

vs

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

The second one works while the first is causing the error. 第二个起作用,而第一个引起错误。 Or the opposite. 或相反。

In my case, that was caused by wrong comment symbols. 就我而言,这是由错误的注释符号引起的。 This is wrong: 这是错误的:

/*{ oldComponent }*/ { newComponent }

This is correct: 这是对的:

{/*{ oldComponent }*/} { newComponent }

Notice the curly brackets 注意大括号

原文链接:https://oldbug.net/q/2JCuZ/Uncaught-Error-Invariant-Violation-Element-type-is-invalid-expected-a-string-for-built-in-components-or-a-class-function-but-got-object import React from 'react'; import ReactDOM from 'react-dom'; import Column2D from './components/Column2D.js' import logo from './logo.svg'; import './App.css'; function App() { 忘记从文件中导出组件错误地定义 React 组件,例如作为变量而不是函数或类。 确保您没有将组件导出为默认导出并尝试将其作为命名导入(用花括号括起来)导入,反之亦然,因为这是导致错误的常见原因。 ❗ 如果在进行必要的更改后错误解决,请确保在必要时重新启动开发服务器和 IDE。 ❗ 您还应该确保指向模块的路径拼写正确、大小写正确以及特定文件导出组件。 确保路径正确的最佳方法是删除它,开始输入路径并让您的 IDE 帮助您自 Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. This error is located at:     in RCTView (at... 从react-native包中引入组件需要用{}包裹,即原文中应该改为 import {Button} from 'react-native'; 我们在react-native中导入组件的时候,如果组件创建的方法中使用了except default class XXX这种形式进行导出,那么我们在导入该组件的时候就不需要用{}包裹组件,如果在导出的时候没有... 错误解决办法:https://blog.csdn.net/niuba123456/article/details/82086513(并不绝对,学会变通) 转载于:https://www.cnblogs.com/dglblog/p/10754736.html Uncaught (in promise) Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in, or 第二种,引入组件后,组件写法有错误,或者夹杂了一些没用的字符,比方说逗号,点,仔细检查组件引入后的使用代码。第一种,export default 没有把组件导出,导致router.js中引入组件时候报该错误。... 前些日子做项目时有一个报错,虽然解决了,但是对于导致的原因,还是一知半解。今天突然发现一篇博客,大受启发,决定将这个问题系统的总结一下。 报错信息: 提示元素类型无效,可能是忘记从你定义的文件中导出来组件,或者是你弄混了要导入的组件的默认名字,没有和你导入时的名字相对应。 解决方式: 1:在导出文件中使用export class 组件类名...