当前位置:   article > 正文

React Native使用AsyncStorage本地持久化_react native asyncstorage.setitem

react native asyncstorage.setitem

AsyncStorage是一个简单的,未加密的,异步的,持久化,关键值存储系统,是全局的。类似于iOS中的NSUserDefault.

存值:

1
2
3
4
5
6
7
import  {
     AppRegistry,
     StyleSheet,
     Text,
     View,
     AsyncStorage
} from  'react-native' ;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
try  {
             AsyncStorage.setItem(
                 'key' ,
                 'shaoting' ,
                 (error)=>{
                     if  (error){
                         alert( '存值失败:' ,error);
                     } else {
                         alert( '存值成功!' );
                     }
                 }
             );
         catch  (error){
             alert( '失败' +error);
         }

  取值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try  {
     AsyncStorage.getItem(
         'key' ,
         (error,result)=>{
             if  (error){
                 alert( '取值失败:' +error);
             } else {
                 alert( '取值成功:' +result);
             }
         }
     )
} catch (error){
     alert( '失败' +error);
}

  移除值:

1
2
3
4
5
6
7
8
9
10
11
12
13
try  {
            AsyncStorage.removeItem(
                'key' ,
                (error)=>{
                    if (!error){
                        alert( '移除成功' );
                    }
                }
            )
        } catch  (error){
            alert( '失败' ,+error);
        }
    },

  完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import  React, { Component } from  'react' ;
import  {
     AppRegistry,
     StyleSheet,
     Text,
     View,
     AsyncStorage
} from  'react-native' ;
 
 
var  Mine = React.createClass({
     render() {
         return  (
             <View style={styles.container}>
                 <Text onPress={ this .saveData} style={styles.welcome}>
                     存值:key= 'key' ,存shaoting
                 </Text>
                 <Text onPress={ this .getValue} style={styles.welcome}>
                     取值
                 </Text>
                 <Text onPress={ this .removeData} style={styles.welcome}>
                     删除数据
                 </Text>
             </View>
         );
     },
     saveData(){
         try  {
             AsyncStorage.setItem(
                 'key' ,
                 'shaoting' ,
                 (error)=>{
                     if  (error){
                         alert( '存值失败:' ,error);
                     } else {
                         alert( '存值成功!' );
                     }
                 }
             );
         catch  (error){
             alert( '失败' +error);
         }
     },
     getValue(){
         try  {
             AsyncStorage.getItem(
                 'key' ,
                 (error,result)=>{
                     if  (error){
                         alert( '取值失败:' +error);
                     } else {
                         alert( '取值成功:' +result);
                     }
                 }
             )
         } catch (error){
             alert( '失败' +error);
         }
     },
     removeData(){
         try  {
             AsyncStorage.removeItem(
                 'key' ,
                 (error)=>{
                     if (!error){
                         alert( '移除成功' );
                     }
                 }
             )
         } catch  (error){
             alert( '失败' ,+error);
         }
     },
});
 
 
var  styles = StyleSheet.create({
     container: {
         flex: 1,
         backgroundColor: 'white' ,
     },
     welcome: {
         fontSize: 20,
         textAlign:  'center' ,
         margin: 10,
     }
});
 
module.exports = Mine;

  运行效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/938464
推荐阅读
相关标签
  

闽ICP备14008679号