我想删除特定的帖子,但是当我单击按钮时什么也没有发生,我不知道自己在想什么
功能
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('posts');
let postsFav = JSON.parse(postsJSON);
postsItems = postsFav.filter(function(e){ return e.post_id == post_id })
AsyncStorage.removeItem('posts', postsItems);
} catch(error) {
}};
纽扣
<Button onPress={this.removePost.bind(this, item.post_id)}>
最佳答案
您可以使用AsyncStorage.setItem()
用posts
更新postItems
。
removePost = async (post_id) => {
try {
const posts = await AsyncStorage.getItem('posts');
let postsFav = JSON.parse(posts);
const postsItems = postsFav.filter(function(e){ return e.post_id !== post_id });
// updating 'posts' with the updated 'postsItems'
await AsyncStorage.setItem('posts', JSON.stringify(postsItems));
} catch(error) {
console.log('error: ', error);
}};
关于javascript - 如何从react-native中的AsyncStorage删除特定项目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50246068/