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

React Native从0.64.3升级到0.73后AppState及AsyncStorage异常求助

React Native 0.64.3 升级至0.73版本后模块缺失问题

报错信息

E  Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'AppState' could not be found. Verify that a module by this name is registered in the native binary.Bridgeless mode: false. TurboModule interop: false. Modules loaded: {"NativeModules":["PlatformConstants","LogBox","Timing"],"TurboModules":[],"NotFound":["NativePerformanceCxx","NativePerformanceObserverCxx","AppState"]}, js engine: hermes
2024-08-02 14:47:59.671 26473-27071 ReactNativeJS           com.example                          E  Error: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

已尝试的解决方案

  • 逐步升级AsyncStorage版本(所有版本均存在该问题)
  • 清除各类缓存
  • 卸载应用后重新构建并重启

可行解决方案

针对AppState缺失问题

  1. 确认AppState导入方式正确:React Native 0.60+版本中AppState已内置,无需额外安装,导入代码应为:
import { AppState } from 'react-native';
  1. 检查Android原生配置:
    • 确认MainApplication.java中已自动注册AppStateModule(RN 0.70+默认自动注册,若升级时原生文件被覆盖,需确保getPackages()方法里包含new AppStatePackage())
    • 执行./gradlew clean清理Android构建缓存后重新构建项目

针对AsyncStorage为null问题

  1. 替换为官方维护的包:卸载旧版@react-native-community/async-storage,安装新版@react-native-async-storage/async-storage:
npm uninstall @react-native-community/async-storage
npm install @react-native-async-storage/async-storage
  1. 确保原生链接正常:
    • iOS:进入ios目录执行pod install,若仍有问题可先删除Pods和Podfile.lock再重新执行
    • Android:RN 0.60+支持自动链接,若未生效,检查settings.gradle是否包含以下配置:
      include ':react-native-async-storage_async-storage'
      project(':react-native-async-storage_async-storage').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-async-storage/async-storage/android')
      

通用排查步骤

  • 核对React Native官方升级指南,确认未遗漏原生文件(如MainApplication.java、gradle.properties、Podfile)的更新步骤
  • 启用TurboModule互操作:在metro.config.js中添加配置,确保TurboModule正常加载:
    module.exports = {
      transformer: {
        enableBabelRuntime: true,
      },
      turboModule: {
        enableInterop: true,
      },
    };
    

内容的提问来源于stack exchange,提问作者hardik web

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 02:22:35