how to remove an item from AsyncStorage in react-native

后端 未结 7 2848
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 21:56

how to remove an item from AsyncStorage? right now I am trying this code

AsyncStorage.removeItem(\'userId\');

but this is not working for me.

7条回答
  •  心在旅途
    2021-02-19 22:24

    This is what I did, had a similar issue.It works well when you want to remove an item based on its id. make sure each item has a unique id.

    remove_user = async(userid) => {
        try{
            let usersJSON= await AsyncStorage.getItem('users');
            let usersArray = JSON.parse(usersJSON);
            alteredUsers = usersArray.filter(function(e){
                return e.id !== userid.id
    
            })
            AsyncStorage.setItem('users', JSON.stringify(alteredUsers));
            this.setState({
               users:alteredUsers
            })
        }
        catch(error){
            console.log(error)
        }
    };
    

提交回复
热议问题