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

React Native更新后出现const声明初始化错误,求助解决

解决React Native 0.77.0中Missing initializer in const declaration错误的方案

问题概述

更新至React Native 0.77.0后,出现SyntaxError: node_modules\react-native\Libraries\Components\ActivityIndicator\ActivityIndicator.js: Missing initializer in const declaration. (156:41)错误,删除重装node_modules无效;手动修改单个组件文件后,Pressable、Button等组件陆续出现同类错误;降级RN版本又触发Kotlin版本冲突问题。

报错代码片段

154 | */
  155 |
> 156 | const ActivityIndicatorWithRef: component(
      |                                          ^
  157 |   ref: React.RefSetter<HostComponent<empty>>,
  158 |   ...props: Props
  159 | ) = React.forwardRef(ActivityIndicator);

环境信息

System:
  OS: Windows 11 10.0.26100
  CPU: (4) x64 11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz
  Memory: 510.18 MB / 7.80 GB
Binaries:
  Node:
    version: 18.17.0
    path: C:\Program Files\nodejs\node.EXE
  Yarn: Not Found
  npm:
    version: 10.9.0
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK:
    API Levels:
      - "31"
      - "32"
      - "33"
      - "33"
      - "34"
      - "35"
    Build Tools:
      - 30.0.3
      - 33.0.0
      - 33.0.1
      - 33.0.2
      - 34.0.0
      - 35.0.0
    System Images:
      - android-30 | Google APIs Intel x86 Atom
      - android-33 | Google APIs Intel x86_64 Atom
    Android NDK: 27.1.12297006
  Windows SDK: Not Found
IDEs:
  Android Studio: Not Found
  Visual Studio: Not Found
Languages:
  Java: javac 20
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 15.1.3
    wanted: ^15.1.3
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.77.0
    wanted: ^0.77.0
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

解决办法

1. 检查Babel配置兼容性

React Native 0.77.0使用的简写类型标注语法,需要对应版本的Babel支持:

  • 确认babel.config.js中包含@babel/preset-typescript,且版本与RN 0.77.0兼容
  • 移除可能干扰默认RN语法转换的自定义Babel插件

2. 彻底清理缓存并重构项目

执行以下命令清理所有缓存并重新安装依赖:

# 清理npm缓存
npm cache clean --force
# 重置Metro缓存
npx react-native start --reset-cache
# 删除依赖包和锁定文件
rm -rf node_modules package-lock.json
# 重新安装依赖
npm install
# 重新构建Android项目
npx react-native run-android

3. 修正TypeScript配置

确保tsconfig.json配置与RN 0.77.0匹配:

  • 设置"target": "ESNext"或"ES2021"
  • 启用"strict": true(可选,提前发现类型问题)
  • 确认"jsx": "react-native"

4. 解决降级后的Kotlin版本冲突

若降级RN版本出现Kotlin错误,在android/build.gradle中指定兼容版本:

buildscript {
    ext {
        kotlinVersion = "1.9.0" // 选择与降级RN版本匹配的Kotlin版本
    }
    // 其他配置...
}

5. 批量修正RN源码语法(临时方案)

若上述方法无效,可临时修改RN源码中的错误语法(不推荐长期使用):
将报错的简写类型标注:

const ComponentWithRef: component(...) = ...

替换为标准TypeScript函数类型:

const ComponentWithRef: React.ForwardRefExoticComponent<Props & React.RefAttributes<HostComponent<empty>>> = React.forwardRef(Component);

内容的提问来源于stack exchange,提问作者Muskan Verma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:56:23