StorageUtil.js 878 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {AsyncStorage} from 'react-native';
  2. export default class StorageUtil {
  3. /**
  4. * 获取
  5. * @param key
  6. * @returns {Promise<T>|*|Promise.<TResult>}
  7. */
  8. static get(key, callback) {
  9. AsyncStorage.getItem(key, (error, object) => {
  10. callback(error, JSON.parse(object));
  11. })
  12. }
  13. /**
  14. * 保存
  15. * @param key
  16. * @param value
  17. * @returns {*}
  18. */
  19. static set(key, value, callback) {
  20. return AsyncStorage.setItem(key, JSON.stringify(value), callback);
  21. }
  22. /**
  23. * 更新
  24. * @param key
  25. * @param value
  26. * @returns {Promise<T>|Promise.<TResult>}
  27. */
  28. static update(key, value) {
  29. StorageUtil.set(key, value);
  30. }
  31. /**
  32. * 删除
  33. * @param key
  34. * @returns {*}
  35. */
  36. static delete(key) {
  37. return AsyncStorage.removeItem(key);
  38. }
  39. /**
  40. * 清除所有Storage
  41. */
  42. static clear() {
  43. AsyncStorage.clear();
  44. }
  45. }