如何在反应原生中将数组保存在异步存储中?

CRo*_*Rod 13 asynchronous local-storage react-native

我正在尝试为以后的列表创建一个保存,哪种类型的存储最适合此异步存储或其他方法?

是否有在此方法中保存数组的示例?

Fuz*_*ree 21

使用JSON.stringifyon save 将数组转换为字符串,然后使用JSON.parseon read 将数组转换为数组:

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
}
Run Code Online (Sandbox Code Playgroud)

来自https://facebook.github.io/react-native/docs/asyncstorage.html的修改示例