asyncStorage.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { AsyncStorage } from "react-native";
  2. export default class DeviceStorage {
  3. static get(key) {
  4. return AsyncStorage.getItem(key).then(value => {
  5. const jsonValue = JSON.parse(value);
  6. return jsonValue;
  7. });
  8. }
  9. static save(key, value) {
  10. return AsyncStorage.setItem(key, JSON.stringify(value));
  11. }
  12. static update(key, value) {
  13. return DeviceStorage.get(key).then(item => {
  14. value =
  15. typeof value === "string" ? value : Object.assign({}, item, value);
  16. return AsyncStorage.setItem(key, JSON.stringify(value));
  17. });
  18. }
  19. static delete(key) {
  20. return AsyncStorage.removeItem(key);
  21. }
  22. }
  23. /**
  24. 使用方法:
  25. 1.先引用asyncStorage.js文件
  26. 2.调用方法
  27. //获取
  28. asyncStorage.get("birthday").then(birthday => {
  29. if (birthday == null || birthday == "") {
  30. consle.log("birthday is null");
  31. } else {
  32. alert("birthday:" + birthday);
  33. }
  34. });
  35. //存储
  36. asyncStorage.save("birthday", this.state.birthday_time);
  37. //更新
  38. asyncStorage.update("birthday","this.state.birthday_time");
  39. //删除
  40. asyncStorage.delete("birthday");
  41. */