React-Native - 为 AsyncStorage 创建抽象层
React-Native - Creating abstraction layer for AsyncStorage
我正在尝试找到一种创建抽象层的方法,以便在 react-native 中使用 AysncStorage
。我试图用 const 方法创建一个 class 但是当我调用它时我得到这个错误:
DOMNodes from htmlparser2 Array [
Object {
"data": "Error Hapened: TypeError: _reactNative.default.setItem is not a function",
"next": null,
"parent": null,
"prev": null,
"type": "text",
},
这是试图访问AysncStorage
的js代码:
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 中创建抽象 class 以与 AsyncStorage
交互的任何想法?
尝试将导入 AysncStorage
的方式更改为以下方式:
import { AsyncStorage } from 'react-native';
我正在尝试找到一种创建抽象层的方法,以便在 react-native 中使用 AysncStorage
。我试图用 const 方法创建一个 class 但是当我调用它时我得到这个错误:
DOMNodes from htmlparser2 Array [
Object {
"data": "Error Hapened: TypeError: _reactNative.default.setItem is not a function",
"next": null,
"parent": null,
"prev": null,
"type": "text",
},
这是试图访问AysncStorage
的js代码:
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 中创建抽象 class 以与 AsyncStorage
交互的任何想法?
尝试将导入 AysncStorage
的方式更改为以下方式:
import { AsyncStorage } from 'react-native';