Browse Source

Video 11: Persist Items with React Native AsyncStorage.

Frederic G. MARAND 7 years ago
parent
commit
b340ed35bd
1 changed files with 14 additions and 0 deletions
  1. 14 0
      app.js

+ 14 - 0
app.js

@@ -1,5 +1,6 @@
 import React, { Component } from "react";
 import {
+  AsyncStorage,
   Keyboard,
   ListView,
   Platform,
@@ -64,6 +65,18 @@ class App extends Component {
     this.setSource = this.setSource.bind(this);
   }
 
+  componentWillMount() {
+    AsyncStorage.getItem("items").then((json) => {
+      try {
+        const items = JSON.parse(json);
+        this.setSource(items, items);
+      }
+      catch (e) {
+
+      }
+    });
+  }
+
   handleAddItem() {
     if (!this.state.value) {
       return;
@@ -165,6 +178,7 @@ class App extends Component {
       dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
       ...otherState
     });
+    AsyncStorage.setItem("items", JSON.stringify(items));
   }
 }