Browse Source

1.提交AsyncStorage(https://reactnative.cn/docs/asyncstorage/#docsNav)。增加本地存储,删除,修改,获取方法。

zhangmengjie 5 years ago
parent
commit
6e21b1bc9b
2 changed files with 51 additions and 1 deletions
  1. 0 1
      pages/components/PersonalInfo.js
  2. 51 0
      pages/utils/asyncStorage.js

+ 0 - 1
pages/components/PersonalInfo.js

@@ -659,7 +659,6 @@ export default class PersonalInfo extends BasePage {
     this.setState({
       birthday_time: year + "年" + month + "月" + day + "日"
     });
-    alert("birthday_time:" + this.state.birthday_time);
   }
 
   photoback(photo_uri) {

+ 51 - 0
pages/utils/asyncStorage.js

@@ -0,0 +1,51 @@
+import { AsyncStorage } from "react-native";
+export default class DeviceStorage {
+  static get(key) {
+    return AsyncStorage.getItem(key).then(value => {
+      const jsonValue = JSON.parse(value);
+      return jsonValue;
+    });
+  }
+  static save(key, value) {
+    return AsyncStorage.setItem(key, JSON.stringify(value));
+  }
+  static update(key, value) {
+    return DeviceStorage.get(key).then(item => {
+      value =
+        typeof value === "string" ? value : Object.assign({}, item, value);
+      return AsyncStorage.setItem(key, JSON.stringify(value));
+    });
+  }
+  static delete(key) {
+    return AsyncStorage.removeItem(key);
+  }
+}
+
+/**
+    使用方法:
+    1.先引用asyncStorage.js文件
+    2.调用方法
+    
+
+
+    //获取
+     asyncStorage.get("birthday").then(birthday => {
+          if (birthday == null || birthday == "") {
+            consle.log("birthday is null");
+          } else {
+            alert("birthday:" + birthday);
+          }
+        });
+
+
+    //存储
+      asyncStorage.save("birthday", this.state.birthday_time);
+
+
+    //更新
+    asyncStorage.update("birthday","this.state.birthday_time");
+
+    //删除
+    asyncStorage.delete("birthday");
+
+ */