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

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


当前回答

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

Parent {
  static something() {
  }
}

Child extends Parent {
}

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

Parent {
  constructor() {}

  static something() {
  }
}

其他回答

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

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

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

如果您正在运行开发监视模式,请停止并重新构建。我将一个ES6模块转换为一个React组件,它只在重建后工作(相对于手表构建)。

我将提供另一个可能的解决方案,一个对我有用的解决方案。我使用便利索引将所有组件收集到一个文件中。

我不相信在写这篇文章的时候,babel是官方支持的,并且会把typescript弄得一团糟——但是我在很多项目中看到过它的使用,而且肯定很方便。

然而,当与继承结合使用时,它似乎会抛出上面问题中提出的错误。

一个简单的解决方案是,对于充当父模块的模块需要直接导入,而不是通过方便的索引文件导入。

. / src /组件/ index.js

export Com1 from './com-1/Com1';
export Com2 from './com-2/Com2';
export Com3 from './com-3/Com3';
export Parent1 from './parent/Parent1';

. / src /组件/ com-1 / Com1.js

import { Com2, Com3 } from '../index';

// This works fine
class Com1 {        
    render() {
        return (
            <div>
                <Com2 {...things} />
                <Com3 {...things} />
            </div>
        );
    }
}

. / src /组件/ com-3 / Com3.js

import { Parent } from '../index';

// This does _not_ work
class Com3 extends Parent {        
}

. / src /组件/ com-3 / Com3.js

import Parent from '../parent/Parent';

// This does work
class Com3 extends Parent {        
}

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

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

可能产生此错误。

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

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

最初,我试图导入React,{组件}从' React ';

我没有注意到我们必须只扩展{Component}