You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native报错:Super expression必须为null或函数,求解决(附代码)

解决React Native中"Super expression must either be null or a function, not undefined"报错

嘿,我一眼就揪出问题所在了!你遇到的这个报错,根源是类组件定义时的一个小语法失误:

你写了 class App extends React.Component() —— 这里不该给React.Component加括号!React.Component是一个类,我们要继承的是这个类本身,而不是调用它的结果。当你加了括号直接调用它时,返回值是undefined(因为构造函数必须用new关键字调用才会返回实例,直接调用的话就会返回undefined),所以JS引擎就抛出了“父类表达式必须是null或者函数,不能是undefined”的错误。

修正后的完整代码:

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

class App extends React.Component { // 去掉这里的括号就好啦
  render(){
    return (
      <View style={styles.container}>
        <Text>Hello World</Text>
      </View>
    );
  }
}

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

额外小建议:

如果想让代码更简洁,还可以解构引入Component:

import React, { Component } from 'react';
// 之后直接写 class App extends Component { ... }

这样修改后,你的App组件就能正常渲染了,快去试试看吧!

内容的提问来源于stack exchange,提问作者Salman Jamil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 08:18:10