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

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


当前回答

对于那些使用react-native的人:

import React, {
  Component,
  StyleSheet,
} from 'react-native';

可能产生此错误。

而直接引用react是更稳定的方法:

import React, { Component } from 'react';
import { StyleSheet } from 'react-native';

其他回答

当我的导入有一个错字时,我得到了这个:

import {Comonent} from 'react';

对于那些使用react-native的人:

import React, {
  Component,
  StyleSheet,
} from 'react-native';

可能产生此错误。

而直接引用react是更稳定的方法:

import React, { Component } from 'react';
import { StyleSheet } from 'react-native';

当你有一个循环依赖时,我看到过这个错误。

class A extends B {}
class B extends C {}
class C extends A {}

我也有同样的问题,把导航器改成{Navigator}

import Navigator from 'react-native-deprecated-custom-components'
// to
import {Navigator} from 'react-native-deprecated-custom-components'

Webpack 4块和哈希与TerserPlugin Uglification

当Webpack通过TerserPlugin(当前版本为1.2.3)使用块和散列并进行缩小和unlification时,就会发生这种情况。在我的例子中,这个错误被缩小到我的供应商的Terser Plugin .[contenthash].js包的丑陋化,它包含了我的node_modules。不使用哈希值时一切正常。

解决方案是在uglification选项中排除bundle:

optimization: {
  minimizer: [
    new TerserPlugin({
      chunkFilter: (chunk) => {
        // Exclude uglification for the `vendors` chunk
        if (chunk.name === 'vendors') {
          return false;
        }
        return true;
      },
    }),
  ],
}

更多信息