Guest User

Untitled

a guest
Sep 20th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. StyleSheet,
  4. Text,
  5. TextInput,
  6. Button,
  7. View,
  8. AsyncStorage
  9. } from 'react-native';
  10.  
  11. export default class AsyncStorageExample extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. name: '',
  16. password: '',
  17. user: null
  18. };
  19. }
  20.  
  21. componentDidMount() {
  22. const res = this.getKey();
  23.  
  24. res.then(data => {
  25. this.setState({ user: JSON.parse(data) });
  26. });
  27. }
  28.  
  29. async getKey() {
  30. try {
  31. const response = await AsyncStorage.getItem(`@MySuperStore:key`);
  32. return response;
  33. } catch (error) {
  34. console.log('Error retrieving data' + error);
  35. }
  36. }
  37.  
  38. async saveKey(value) {
  39. try {
  40. const response = await AsyncStorage.setItem(
  41. '@MySuperStore:key',
  42. JSON.stringify(value)
  43. );
  44. return response;
  45. } catch (error) {
  46. console.log('Error saving data' + error);
  47. }
  48. }
  49.  
  50. async resetKey() {
  51. try {
  52. await AsyncStorage.removeItem('@MySuperStore:key');
  53. const response = await AsyncStorage.getItem('@MySuperStore:key');
  54. return response;
  55. } catch (error) {
  56. console.log('Error resetting data' + error);
  57. }
  58. }
  59.  
  60. onChange = (key, value) => {
  61. this.setState({ [key]: value });
  62. };
  63.  
  64. login = () => {
  65. const { email, password } = this.state;
  66. const user = { id: Date.now(), email, password };
  67. const res = this.saveKey(user);
  68.  
  69. res.then(() => {
  70. console.log(user);
  71. this.setState({ user });
  72. });
  73. };
  74.  
  75. logout = () => {
  76. const res = this.resetKey();
  77.  
  78. res.then(data => {
  79. console.log('logout', data);
  80. this.setState({ user: null });
  81. });
  82. };
  83.  
  84. render() {
  85. return (
  86. <View style={styles.container}>
  87. {!this.state.user ? (
  88. <View>
  89. <Text style={styles.welcome}>Welcome to Demo AsyncStorage!</Text>
  90.  
  91. <TextInput
  92. style={styles.formInput}
  93. placeholder="Name"
  94. value={this.state.email}
  95. onChangeText={email => this.onChange('email', email)}
  96. />
  97.  
  98. <TextInput
  99. style={styles.formInput}
  100. placeholder="Enter key you want to save!"
  101. value={this.state.password}
  102. onChangeText={password => this.onChange('password', password)}
  103. />
  104.  
  105. <Button
  106. style={styles.formButton}
  107. onPress={this.login}
  108. title="Login"
  109. color="#2196f3"
  110. accessibilityLabel="Login"
  111. />
  112. </View>
  113. ) : (
  114. <View>
  115. <Button
  116. style={styles.formButton}
  117. onPress={this.logout}
  118. title="Logout"
  119. color="#f44336"
  120. accessibilityLabel="Logout"
  121. />
  122. <View>
  123. <Text>User</Text>
  124. <Text>id: {this.state.user.id}</Text>
  125. <Text>email: {this.state.user.email}</Text>
  126. <Text>password: {this.state.user.password}</Text>
  127. </View>
  128. </View>
  129. )}
  130. </View>
  131. );
  132. }
  133. }
  134.  
  135. const styles = StyleSheet.create({
  136. container: {
  137. padding: 30,
  138. flex: 1,
  139. alignItems: 'stretch',
  140. backgroundColor: '#F5FCFF'
  141. },
  142. welcome: {
  143. fontSize: 20,
  144. textAlign: 'center',
  145. margin: 10
  146. },
  147. formInput: {
  148. paddingLeft: 5,
  149. height: 50,
  150. borderWidth: 1,
  151. borderColor: '#555555'
  152. },
  153. formButton: {
  154. borderWidth: 1,
  155. borderColor: '#555555',
  156. marginBottom: 15
  157. }
  158. });
Add Comment
Please, Sign In to add comment