Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { PureComponent } from 'react';
  2. import { View, Text, TouchableOpacity } from 'react-native';
  3. import AsyncStorage from '@react-native-community/async-storage';
  4.  
  5. export class Home extends PureComponent<{}> {
  6.   private saveData = async () => {
  7.     try {
  8.       await AsyncStorage.setItem(
  9.         '@appdemo:tasks',
  10.         JSON.stringify([{ key: 1, text: 'uno' }, { key: 2, text: 'dos' }])
  11.       );
  12.     } catch (error) {
  13.       console.warn(error);
  14.     }
  15.   };
  16.  
  17.   private recoverData = async () => {
  18.     try {
  19.       const valor = await AsyncStorage.getItem('@appdemo:tasks');
  20.       console.warn(valor);
  21.  
  22.       if (valor !== null) {
  23.         const newtask = JSON.parse(valor);
  24.         this.setState({ tareas: newtask });
  25.       }
  26.     } catch (error) {
  27.       console.warn(error);
  28.     }
  29.   };
  30.  
  31.   public render(): JSX.Element {
  32.     return (
  33.       <View style={{ flex: 1, paddingTop: 50 }}>
  34.         <TouchableOpacity onPress={this.saveData}>
  35.           <Text>Save</Text>
  36.         </TouchableOpacity>
  37.         <TouchableOpacity onPress={this.recoverData}>
  38.           <Text>Retrieve</Text>
  39.         </TouchableOpacity>
  40.         <Text>{JSON.stringify(this.state, null, 2)}</Text>
  41.       </View>
  42.     );
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement