【问题标题】:Two instances of AsyncStorage in React Native?React Native 中的两个 AsyncStorage 实例?
【发布时间】:2021-09-12 04:33:17
【问题描述】:

我可以有两个 AsynStorage 实例吗?我知道当你设置一个项目时,你可以例如对一个数组进行字符串化并有类似的东西:

AsyncStorage.setItem( 'names' , '[John, Carl, Sam]' );

因为我知道您不能将对象或数组保存为 AsyncStorage 中的值。那么有没有办法让多个实例拥有这样的东西:

Instance 1 AsyncStorage:
-["a","aaa"]
-["b","bbb"]
-["c","ccc"]

Instance 2 AsyncStorage:
-["d","ddd"]
-["e","eee"]
-["f","fff"]

并调用例如 Instance2AsyncStorage.getItem("d") ,如果我想要实例 1 中的某些内容,我会执行 Instance1AsyncStorage.getItem("a")?

【问题讨论】:

    标签: react-native asyncstorage


    【解决方案1】:

    您应该将其转换为字符串并保存,然后当您想使用它时,您可以解析它。例如:

    const arr = ["a", "b"];
    // First you should convert it to string and then save it
    await AsyncStorage.setItem('names' , JSON.stringify(arr));
    
    // Then you can do this for using it:
    const response = await AsyncStorage.getItem('names');
    const result = JSON.parse(response);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2019-01-03
      • 2019-11-23
      • 1970-01-01
      相关资源
      最近更新 更多