异步存储返回函数而不是值
Async Storage returning function instead of values
我正在尝试在我的 React 本机应用程序中使用 AsyncStorage,但不知道为什么不起作用。
基本上,我希望将一个索引数组(或任何 key-value 对)存储在异步存储中,并为已添加的每个元素设置 true 或 false。
import {AsyncStorage} from 'react-native';
.....
componentDidMount() {
this.storeData('favourites', []);
}
addOrRemove(id) {
let favourites = this.getData('favourites');
console.log('favourites getted: ', favourites);
favourites[id] = favourites[id] ? false : true; //this logic is working fine
this.storeData('favourites', favourites);
}
获取数据和存储数据:
storeData = (key, value) => async () => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
// saving error
}
};
getData = key => async () => {
try {
const value = await AsyncStorage.getItem(key)
return value;
} catch(e) {
// error reading value
}
};
这就是我做 console.log('favourites getted: ', 收藏夹);
favourites getted: function _callee2() {
var value;
return _regenerator.default.async(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 3;
return _regenerator.default.awrap(_reactNative.AsyncStorage.getItem(key));
case 3:
value = _context2.sent;
return _context2.abrupt("return", value);
case 7:
_context2.prev = 7;
_context2.t0 = _context2["catch"](0);
case 9:
case "end":
return _context2.stop();
}
}
}, null, null, [[0, 7]]);
}
当有人点击特定按钮时,方法 addOrRemove(id)
被触发。我想获取存储在我的 AsyncStorage 中的数组,并将 true 或 false 放在该数组的 id 位置。
为什么我从 AsyncStorage 接收该函数而不是我想要的索引数组?
我认为这可能是一个 async/await 问题,但不知道问题出在哪里。
你的函数"storeData"和"getData"return一个异步函数,你可以简化一下:
storeData = async (key, value) => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
// process error
}
};
getData = async (key) => {
try {
const value = await AsyncStorage.getItem(key)
return value;
} catch (e) {
// process error
}
};
并将它们与 async/await 一起使用:
componentDidMount() {
this.storeData('favourites', []);
}
async addOrRemove(id) {
try {
let favourites = await this.getData('favourites');
console.log('favourites getted: ', favourites);
favourites[id] = favourites[id] ? false : true;
await this.storeData('favourites', favourites);
} catch (err) {
//process error
}
}
我正在尝试在我的 React 本机应用程序中使用 AsyncStorage,但不知道为什么不起作用。
基本上,我希望将一个索引数组(或任何 key-value 对)存储在异步存储中,并为已添加的每个元素设置 true 或 false。
import {AsyncStorage} from 'react-native';
.....
componentDidMount() {
this.storeData('favourites', []);
}
addOrRemove(id) {
let favourites = this.getData('favourites');
console.log('favourites getted: ', favourites);
favourites[id] = favourites[id] ? false : true; //this logic is working fine
this.storeData('favourites', favourites);
}
获取数据和存储数据:
storeData = (key, value) => async () => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
// saving error
}
};
getData = key => async () => {
try {
const value = await AsyncStorage.getItem(key)
return value;
} catch(e) {
// error reading value
}
};
这就是我做 console.log('favourites getted: ', 收藏夹);
favourites getted: function _callee2() {
var value;
return _regenerator.default.async(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 3;
return _regenerator.default.awrap(_reactNative.AsyncStorage.getItem(key));
case 3:
value = _context2.sent;
return _context2.abrupt("return", value);
case 7:
_context2.prev = 7;
_context2.t0 = _context2["catch"](0);
case 9:
case "end":
return _context2.stop();
}
}
}, null, null, [[0, 7]]);
}
当有人点击特定按钮时,方法 addOrRemove(id)
被触发。我想获取存储在我的 AsyncStorage 中的数组,并将 true 或 false 放在该数组的 id 位置。
为什么我从 AsyncStorage 接收该函数而不是我想要的索引数组?
我认为这可能是一个 async/await 问题,但不知道问题出在哪里。
你的函数"storeData"和"getData"return一个异步函数,你可以简化一下:
storeData = async (key, value) => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
// process error
}
};
getData = async (key) => {
try {
const value = await AsyncStorage.getItem(key)
return value;
} catch (e) {
// process error
}
};
并将它们与 async/await 一起使用:
componentDidMount() {
this.storeData('favourites', []);
}
async addOrRemove(id) {
try {
let favourites = await this.getData('favourites');
console.log('favourites getted: ', favourites);
favourites[id] = favourites[id] ? false : true;
await this.storeData('favourites', favourites);
} catch (err) {
//process error
}
}