如何在本机反应中从 asyncStorage 中删除单个项目?
How to remove a single item from asyncStorage in react native?
我有一个用 asyncStorage 存储的项目列表,它们都有相同的键,使用 await AsyncStorage.removeItem(key)
函数删除整个列表
有没有办法只删除一个项目。
async removeItemValue(key) {
try {
await AsyncStorage.removeItem(key);
return true;
}
catch(exception) {
return false;
}
使用
static removeItem(key: string, [callback]: ?(error: ?Error) => void)
参考:
https://facebook.github.io/react-native/docs/asyncstorage.html
我会做什么:
- 获取列表
- 删除项目
- 保存新列表。
类似于:
const value = await AsyncStorage.getItem(theKey);
if (value !== null){
var index = array.indexOf(theItem);
if (index > -1){
value.splice(index, 1);
}
}
AsyncStorage.setItem(theKey, value);
我有一个用 asyncStorage 存储的项目列表,它们都有相同的键,使用 await AsyncStorage.removeItem(key)
函数删除整个列表
有没有办法只删除一个项目。
async removeItemValue(key) {
try {
await AsyncStorage.removeItem(key);
return true;
}
catch(exception) {
return false;
}
使用 static removeItem(key: string, [callback]: ?(error: ?Error) => void)
参考: https://facebook.github.io/react-native/docs/asyncstorage.html
我会做什么:
- 获取列表
- 删除项目
- 保存新列表。
类似于:
const value = await AsyncStorage.getItem(theKey);
if (value !== null){
var index = array.indexOf(theItem);
if (index > -1){
value.splice(index, 1);
}
}
AsyncStorage.setItem(theKey, value);