我正在使用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。Component,而不是React. Component或React。createComponent,你可能需要升级你的React版本。有关要扩展的类的更多信息,请参阅下面的回答。
升级的反应
React从0.13.0版本开始只支持es6风格的类(请参阅他们的官方博客中关于支持介绍的文章)。
在此之前,使用时:
class HelloMessage extends React.Component
你试图使用ES6关键字(扩展)从一个没有使用ES6类定义的类的子类。这可能就是为什么你会遇到一些带有超定义的奇怪行为。
所以,是的,TL;DR -更新到React v0.13.x。
循环依赖
如果您有循环导入结构,也会发生这种情况。一个模块导入另一个模块,反之亦然。在这种情况下,您只需要重构代码以避免这种情况。更多信息
我将提供另一个可能的解决方案,一个对我有用的解决方案。我使用便利索引将所有组件收集到一个文件中。
我不相信在写这篇文章的时候,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, {
AppRegistry,
Component,
Text,
View,
TouchableHighlight
} from 'react-native';
而不是
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
AsyncStorage
} from 'react-native';
这个答案不正确,但对于其他有同样错误的人,我这个愚蠢的错误可能会有帮助。
愚蠢的是,我的问题是通过在类名后面包含()来使用函数符号。
确保语法正确。并且您已经导入和导出了所有具有正确名称和路径的外部组件/模块。
如果你安装了一个新版本的react,这个模板应该可以正常工作:
import React, { Component } from 'react'
class ExampleClass extends Component {
render(){
return(
<div>
</div>
)
}
}
export default ExampleClass
在我的例子中,我使用了一个具有对等依赖关系的npm模块。该错误是由模块的webpack配置中错误的“外部”配置引起的:
externals: {
react: 'react',
react: 'prop-types',
},
它应该是:
externals: {
react: 'react',
['prop-types']: 'prop-types',
},
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;
},
}),
],
}
更多信息
我已经看到这个错误发生由于'评论'在包生成的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
}
}
}),
)],
}
}