-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
126 lines (109 loc) · 2.68 KB
/
index.ios.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
ScrollView,
AsyncStorage,
View
} from 'react-native';
var STORAGE_KEY_ONE = '@haofeng:key_one';
export default class haofeng extends Component {
constructor (props) {
super(props)
this.state = {
ToDos:[]
}
}
//组件挂载之后回调方法
componentDidMount(){
this._didMount = true;
this._loadInitialState().done();
}
//初始化数据-默认从AsyncStorage中获取数据
async _loadInitialState(){
try{
var value=await AsyncStorage.getItem(STORAGE_KEY_ONE);
if(value!=null){
var obj = JSON.parse(value);
this.setState( obj);
}
}catch(error){
}
}
//进行储存数据_ONE
async _saveValueOne(){
try{
var str =JSON.stringify(this.state);
console.log("aaaaaa"+str);
await AsyncStorage.setItem(STORAGE_KEY_ONE,str);
}catch(error){
console.log(error);
}
}
//进行把message信息添加到messages数组中
_appendMessage(ToDos){
this.setState({ToDos:this.state.ToDos.concat(ToDos)});
}
addTask (task) {
let newTask = {
key: Date.now(),
name: task
}
let toDos = this.state.ToDos
console.log(toDos)
toDos.push(newTask)
this.setState({ToDos: toDos})
}
deleteTask(key) {
let todos = this.state.ToDos
let newtodos = todos.filter(task => task.key != key)
this.setState({ToDos: newtodos})
}
clearText () {
this._textInput.setNativeProps({text:''})
}
render() {
this._didMount && this._saveValueOne();
return (
<View style={styles.container} >
<Text >
Welcome To My Life
</Text>
<TextInput
placeholder='say something'
style={{width: 200, height: 40}}
onEndEditing={task => {
this.addTask(task.nativeEvent.text)
this.clearText.call(this)
}}
ref = { component => this._textInput = component}
/>
<Text style={styles.content}>
</Text>
<ScrollView>
{this.state.ToDos.map(task => <Text key={task.key}
onPress={ () => this.deleteTask(task.key)}
>{task.name}
</Text>)}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: 50,
flex: 1,
justifyContent: 'flex-start',
alignItems: 'flex-start',
backgroundColor: '#F5FCFF',
},
content:{
fontSize: 13,
textAlign: 'left',
margin: 10,
}
});
AppRegistry.registerComponent('haofeng', () => haofeng);