how to remove an item from AsyncStorage in react-native

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

    This is the framework code for AsyncStorage.removeItem:

    removeItem: function(
     key: string,
     callback?: ?(error: ?Error) => void
    ): Promise {
     return new Promise((resolve, reject) => {
      RCTAsyncStorage.multiRemove([key], function(errors) {
        var errs = convertErrors(errors);
        callback && callback(errs && errs[0]);
        if (errs) {
          reject(errs[0]);
        } else {
          resolve(null);
        }
      });
     });
    }
    

    As you can see above it requires the key(Which is name of the item you set in asyncstorage that you want to delete) and a callback function. Make sure you have the correct key and the correct params and it should work fine.

提交回复
热议问题