我正在try 创建一个"保存以备将来使用"列表,对于这种异步存储或其他方法,什么类型的存储是最好的?

有没有用这种方法保存数组的例子?

推荐答案

保存时使用JSON.stringify将数组转换为字符串,读取时使用JSON.parse将数组转换为字符串:

var myArray = ['one','two','three'];

try {
  await AsyncStorage.setItem('@MySuperStore:key', JSON.stringify(myArray));
} catch (error) {
  // Error saving data
}

try {
  const myArray = await AsyncStorage.getItem('@MySuperStore:key');
  if (myArray !== null) {
    // We have data!!
    console.log(JSON.parse(myArray));
  }
} catch (error) {
  // Error retrieving data
}

修改了https://react-native-async-storage.github.io/async-storage/docs/install/的例子

React-native相关问答推荐

应用管理

在 React Native 中打开后日期 Select 器不关闭

到达安装依赖项时 EAS 构建错误

错误:HostFunction 中的异常: Malformed calls from JS: field sizes are different. In an Animated View

React Native Android:how to remove "overscroll effect" of ScrollView

react native foreach 循环

异步(async)/等待(await)和递归

自从升级到 Xcode 10.2 我不能再通过 cli 运行 react-native run-ios

library not found for -lRCTGeolocation

Firebase检测用户是否存在

在react native 中出现Cannot read property 'pick算法rithm' of null错误

无法加载 exp:// 出了点问题

react-native async 函数返回 promise 但不返回我的 json 数据?

React Native - React Navigation导航转换

如何用 Realm 元素文件组织 React Native?

由于对 sourcetree 的 husky 预推送,Git 推送失败

带有 React Native 的 EventEmitter 和 Subscriber ES6 语法

React-Native Invariant Violation:元素类型无效

无法运行 jetifier React Native

react-native构建错误:package android.support.annotation does not exist