我想删除特定帖子,但是当我单击按钮时没有任何反应,我不知道我错过了什么
函数
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()
使用 postItems
更新 posts
。
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/