HarmonyOS 鸿蒙Next中RN使用AsyncStorage报错

HarmonyOS 鸿蒙Next中RN使用AsyncStorage报错 我使用metro调试开发,出现了报错,该怎么排查啊?

TypeError: _RCTAsyncStorage.default.multiGet is not a function (it is undefined)
TypeError: _RCTAsyncStorage.default.multiGet is not a function (it is undefined)
at anonymous (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:126407:44)
at tryCallTwo (/Users/admin/Desktop/rnoh/publish/build_release/arm64-v8a-output/lib/InternalBytecode/InternalBytecode.js:61:9)
at doResolve (/Users/admin/Desktop/rnoh/publish/build_release/arm64-v8a-output/lib/InternalBytecode/InternalBytecode.js:216:25)
at Promise (/Users/admin/Desktop/rnoh/publish/build_release/arm64-v8a-output/lib/InternalBytecode/InternalBytecode.js:82:14)
at getItem (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:126405:27)
at getItem (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:125799:58)
at Storage (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:125792:38)
at anonymous (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:124993:48)
at loadModuleImplementation (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:328:14)
at guardedLoadModule (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:227:38)
at metroRequire (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:123:92)
at anonymous (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:122961:113)
at loadModuleImplementation (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:328:14)
at guardedLoadModule (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:227:38)
at metroRequire (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:123:92)
at anonymous (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:122894:109)
at loadModuleImplementation (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:328:14)
at guardedLoadModule (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:227:38)
at metroRequire (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:123:92)
at anonymous (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:1465:104)
at loadModuleImplementation (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:328:14)
at guardedLoadModule (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:219:47)
at metroRequire (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:123:92)
at global (http://localhost:8081/index.bundle//&platform=harmony&dev=true&minify=false:261681:4)

更多关于HarmonyOS 鸿蒙Next中RN使用AsyncStorage报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

这边没有复现你的问题,你可以参考文档中的步骤进行开发, https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-async-storage-async-storage.md

参考demo

import React,{useState} from 'react';
import AsyncStorage from "@react-native-async-storage/async-storage";
import { View,Text,Button } from "react-native-harmony";

// Storing data
export function AsyncDemo(): JSX.Element{
  const [storageValue,setStorageValue] = useState("张三")
  const storeData = async () => {
    try {
      await AsyncStorage.setItem("my-key", "李四");
    } catch (e) {

    }
  };

  // Reading data
  const getData = async () => {
    try {
      const value = await AsyncStorage.getItem("my-key");

      if (value !== null) {
        setStorageValue(value)
      }

    } catch (e) {

    }
  };
  return 
<Button onPress={storeData} title="设置storage!" />
<Button onPress={getData} title="获取storage!" />
<Text style={{color:"#fff"}}>{storageValue}</Text>
}

更多关于HarmonyOS 鸿蒙Next中RN使用AsyncStorage报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,React Native (RN) 使用 AsyncStorage 报错可能与鸿蒙系统的适配问题有关。AsyncStorage 是React Native中用于异步存储数据的API,但在鸿蒙系统中,由于其独特的系统架构和API支持,可能会导致兼容性问题。

具体来说,鸿蒙Next系统与Android系统在底层实现上有显著差异,尤其是在文件系统和数据存储相关的API上。AsyncStorage 在Android上通常依赖于本地存储的SQLite数据库,而鸿蒙系统可能使用了不同的存储机制或API,导致 AsyncStorage 无法正常工作。

此外,鸿蒙Next系统对JavaScript引擎的支持也可能与React Native的预期有所不同。React Native依赖于JavaScriptCore或Hermes引擎来执行JavaScript代码,而鸿蒙系统可能使用了不同的JavaScript引擎或运行时环境,这可能导致 AsyncStorage 的实现无法正确运行。

解决这类问题通常需要查看具体的错误日志,并根据日志信息调整代码或寻找适合鸿蒙系统的替代方案。如果错误涉及到API不兼容或缺失,可能需要使用鸿蒙系统提供的原生API来实现类似的功能。

总之,鸿蒙Next系统中 AsyncStorage 报错的原因可能与其系统架构、存储机制或JavaScript引擎的差异有关,具体解决方案需要根据错误信息进一步分析。

在HarmonyOS 鸿蒙Next中使用React Native的AsyncStorage时,可能会遇到报错问题。通常这是由于鸿蒙系统与React Native的兼容性问题导致的。建议检查以下几点:

  1. 版本兼容性:确保使用的React Native和AsyncStorage版本与鸿蒙系统兼容。
  2. 依赖安装:确认AsyncStorage已正确安装并导入到项目中。
  3. 权限配置:检查是否已正确配置存储权限。
  4. 调试日志:查看具体报错信息,定位问题所在。

如果问题仍未解决,建议参考鸿蒙官方文档或社区论坛,获取更多技术支持。

回到顶部