gpt4 book ai didi

javascript - 如何在react-native中从AsyncStorage中删除特定项目?

转载 作者:行者123 更新时间:2023-11-28 10:42:05 27 4
gpt4 key购买 nike

我想删除特定帖子,但是当我单击按钮时没有任何反应,我不知道我错过了什么

函数

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com