how to remove an item from AsyncStorage in react-native

后端 未结 7 3019
被撕碎了的回忆
被撕碎了的回忆 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:32

    this delete method which removes object from array by passing index (here i called id)

    async deleteData(id)  {
          try {
            this.state.item.splice(id, 1);
            await AsyncStorage.setItem("mylist",JSON.stringify(this.state.item))
            this.setState({ item: JSON.parse(await AsyncStorage.getItem("mylist")) })
    
          } catch (error) {
            console.log(error);
          }
        };
    

    and call this by using onPress method, here i am using button and pass index

    
    

提交回复
热议问题