-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncStorageTest.js
89 lines (83 loc) · 2.48 KB
/
AsyncStorageTest.js
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
/**
* Created by Administrator on 2017/3/15 0015.
*/
import React, { Component } from 'react';
import {
Text,
TextInput,
View,
StyleSheet,
Image,
AsyncStorage,
} from 'react-native';
import NavigatorBar from './js/common/NavigatorBar';
import Toast,{DURATION} from 'react-native-easy-toast'
export default class AsyncStorageTest extends Component{
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
};
}
onSave(){
AsyncStorage.setItem('KEY',this.text,(err)=>{
if(!err){
this.toast.show('保存成功',DURATION.LENGTH_LONG);
}else{
this.toast.show('保存失败',DURATION.LENGTH_LONG);
}
})
}
onFecth(){
AsyncStorage.getItem('KEY',(err,result)=>{
if(!err){
if(result!==''&&result!==null){
this.toast.show('取出内容为'+result);
}else{
this.toast.show('取出内容不存在');
}
}else{
this.toast.show('取出失败');
}
})
}
onRmove(){
AsyncStorage.removeItem('KEY',(err)=>{
if(!err){
this.toast.show('移除成功',DURATION.LENGTH_LONG);
}else{
this.toast.show('移除失败',DURATION.LENGTH_LONG);
}
})
}
render(){
return <View style={styles.container}>
<NavigatorBar title='AsyncStorage的运用' justifyContent='center'/>
<TextInput style={{borderWidth:1,height:40}}
onChangeText={(text)=>this.text=text}
/>
<View style={{flexDirection:'row',justifyContent:'center',height:40,alignItems:"center" }}>
<Text style={{flex:1,textAlign:'center',fontSize:30}}
onPress={()=>this.onSave()}
>保存</Text>
<Text style={{flex:1,textAlign:'center',fontSize:30}}
onPress={()=>this.onRmove()}
>移除</Text>
<Text style={{flex:1,textAlign:'center',fontSize:30}}
onPress={()=>this.onFecth()}
>取出</Text>
</View>
<Toast ref={toast=>this.toast=toast} />
</View>
}
}
const styles=StyleSheet.create({
container:{
flex:1,
// backgroundColor:'gray'
},
text:{
fontSize:20
}
})