我正在尝试找到一种方法来创建一个抽象层,以便在react-native中使用AysncStorage
。我试图用const方法创建一个类,但是当我调用它时出现此错误:
DOMNodes from htmlparser2 Array [
Object {
"data": "Error Hapened: TypeError: _reactNative.default.setItem is not a function",
"next": null,
"parent": null,
"prev": null,
"type": "text",
},
这是JS尝试访问AysncStorage
的代码:
import AsyncStorage from 'react-native';
export const getClient = () => {
return _retrieveData("loggedClient");
}
export const setClient = (client) => {
_storeData("loggedClient", JSON.stringify(client));
}
export const clear = () => {
AsyncStorage.clear();
}
_retrieveData = async (id) => {
try {
const value = await AsyncStorage.getItem("@App:"+id);
if (value !== null) {
// We have data!!
console.log("found: " + id + "="+ value);
return value;
} else {
console.log("Not found: " + id);
}
} catch (error) {
console.log("Failure retrieving "+ id +" error: " + error)
}
};
_storeData = async (id, value) => {
try {
await AsyncStorage.setItem('@App:'+id, value);
console.log("saved");
} catch (error) {
console.log("Failure saving: " + error);
}
};
关于如何在react-native中创建抽象类以与AsyncStorage
进行交互的任何想法?
答案 0 :(得分:1)
尝试将导入AysncStorage
的方式更改为以下内容:
import { AsyncStorage } from 'react-native';