我正在使用ReactJS。

当我运行下面的代码时,浏览器会显示:

Uncaught TypeError:超级表达式必须为null或函数,不能为undefined

任何关于哪里出了问题的提示都会让我感激不尽。

首先是用来编译代码的行:

browserify -t reactify -t babelify examples/temp.jsx  -o examples/public/app.js

代码是:

var React = require('react');

class HelloMessage extends React.Component {
  render() {
    return <div>Hello </div>;
  }
}

更新: 在这个问题上在地狱火里燃烧了三天之后,我发现我没有使用react的最新版本。

全球安装:

sudo npm install -g react@0.13.2

在本地安装:

npm install react@0.13.2

确保浏览器使用正确的版本:

<script type="text/javascript" src="react-0.13.2.js"></script>

希望这能挽救别人三天宝贵的生命。


当前回答

我已经看到这个错误发生由于'评论'在包生成的webpack。在webpack.config.js中使用'pathinfo'= true会导致以下问题:

webpack.config.js

module.exports = {
  output: {
    pathinfo: true,
  }
}

'pathinfo'在开发中默认为true,在生产中默认为false 模式。https://webpack.js.org/configuration/output/#outputpathinfo 尝试将此值设置为false以解决问题。

如果您没有使用插件从构建输出中剥离注释,也会发生这种情况。尝试使用UglifyJs (https://www.npmjs.com/package/uglifyjs-webpack-plugin/v/1.3.0):

webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  ...
  optimization: {
    minimizer: [new UglifyJsPlugin(
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false
          }
        }
      }),
    )],
  }
}

其他回答

如果你试图执行React,你也可以收到这个消息。组件在类定义中带有error()。

export default class MyComponent extends React.Component() {}
                                                        ^^ REMOVE

在从无状态函数组件转换到类时,我有时会设法做到这一点。

这招对我很管用:

import React, {Component} from 'react';

在我们的例子中,我们试图扩展一个只有静态函数的父类。即。

Parent {
  static something() {
  }
}

Child extends Parent {
}

向Parent添加一个构造函数就解决了这个问题。

Parent {
  constructor() {}

  static something() {
  }
}

这意味着你想要子类化一些东西,它应该是Class,但没有定义。原因可能是:

类扩展中的拼写错误…,所以你扩展了一个未定义的变量 输入错误…从,所以你导入了undefined From模块 引用的模块不包含该值,你想导入(例如,过时的模块-情况与React),所以你导入不存在的值(未定义) 引用模块导出时出现错误…语句,因此它导出未定义的变量 引用的模块完全没有导出语句,所以它导出的是未定义的

对我来说,是React。元素更改为React。修复此错误的组件。