-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.js
85 lines (79 loc) · 2.06 KB
/
App.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
import React from 'react';
import { StyleSheet, Text, TextInput, View, Button, AsyncStorage } from 'react-native';
import AsyncStorageREPL from './src/rn/AsyncStorageREPL';
AsyncStorageREPL().connect();
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { inputText: '', comments: [] };
this._handlePost = this.handlePost.bind(this);
}
componentDidMount() {
AsyncStorage.getItem('comments', (_err, result) => {
this.setState({ comments: JSON.parse(result) || [] });
});
}
handlePost() {
const comments = this.state.comments.concat(this.state.inputText);
AsyncStorage.setItem('comments', JSON.stringify(comments), (_err) => {
this.setState({ comments, inputText: '' });
});
}
render() {
const comments = this.state.comments.map((comment, idx) =>
<Text key={idx} style={styles.comment}>{comment}</Text>);
return (
<View style={styles.container}>
<View style={styles.postArea}>
<View>
<TextInput
style={styles.input}
value={this.state.inputText}
onChangeText={(text) => { this.setState({ inputText: text }); }}
/>
</View>
<Button onPress={this._handlePost} title="POST" />
</View>
<View style={styles.commentArea}>
<Text style={styles.title}>Comments</Text>
{comments}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: 60,
marginHorizontal: 20,
flex: 1,
},
input: {
padding: 8,
width: 300,
height: 40,
borderColor: 'gray',
borderWidth: 1,
},
postArea: {
marginBottom: 16,
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: 'white',
},
commentArea: {
flex: 5,
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'flex-start',
backgroundColor: 'white',
},
title: {
fontSize: 20,
},
comment: {
margin: 8,
},
});