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

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


当前回答

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

我不相信在写这篇文章的时候,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 {        
}

其他回答

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

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

这个答案不正确,但对于其他有同样错误的人,我这个愚蠢的错误可能会有帮助。

愚蠢的是,我的问题是通过在类名后面包含()来使用函数符号。

确保语法正确。并且您已经导入和导出了所有具有正确名称和路径的外部组件/模块。

如果你安装了一个新版本的react,这个模板应该可以正常工作:

import React, { Component } from 'react'

class ExampleClass extends Component {

    render(){
        return(
            <div>

            </div>
        )
    }
}

export default ExampleClass 

我也有同样的问题因为"反应时刻"

将其替换为'moment'库

在我的例子中,我使用的是:

class Root extends React.Component() {
// THIS IS WORNG
// After React.Component () <- WRONG!!
}

这是错误的但正确的是:

class Root extends React.Component {
// THIS IS CORRECT
// That -> '()' after React.Component was typo
}

也要确保有 √反应。组件 不 ˣReact.component或React。组件

问题似乎是这样的 反应。组件而不是React.Component。

还有,ReactDOM是你写的吗?render(, YourNode) here?

希望你的问题解决了。干杯!