React Native 中的两个 AsyncStorage 实例?
Two instances of AsyncStorage in React Native?
我可以拥有两个 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")?
你应该把它转换成字符串并保存,然后当你想使用它时你可以解析它。例如:
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);
我可以拥有两个 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")?
你应该把它转换成字符串并保存,然后当你想使用它时你可以解析它。例如:
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);