我正在使用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>

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


当前回答

如果你收到这个错误并且正在使用Browserify和Browserify -shim(就像在Grunt任务中),你可能会像我一样经历一个愚蠢的时刻,你无意中告诉Browserify -shim将React视为已经是全局命名空间的一部分(例如,从CDN加载)。

如果你想让Browserify包含React作为转换的一部分,而不是一个单独的文件,确保行" React": "global:React"不会出现在你的包中的" Browserify -shim"部分。json文件。

其他回答

我已经看到这个错误发生由于'评论'在包生成的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
          }
        }
      }),
    )],
  }
}

如果您在导入或类生成中有拼写错误,可能只是这样。

当JSX类缺少一个导出语句时,我就经历过这种情况。

例如:

class MyComponent extends React.Component {
}
export default MyComponent // <- add me

使用Babel(5.8),如果我尝试将表达式export default与其他一些导出结合使用,我会得到相同的错误:

export const foo = "foo"
export const bar = "bar"
export default function baz() {}

对其他人来说,这可能会导致这个问题。你也可以检查React中的component方法。Component是大写的。我也有同样的问题,原因是我写道:

class Main extends React.component {
  //class definition
}

我把它改成

class Main extends React.Component {
  //class definition
}

一切都很顺利