-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
89 lines (84 loc) · 2.2 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
86
87
88
89
import React from 'react';
import { StyleSheet, FlatList, SafeAreaView,AsyncStorage } from 'react-native';
import Header from './components/Header'
import TodoItem from './components/TodoItem'
import TaskModal from './components/TaskModal'
export default class App extends React.Component {
componentDidMount() {
AsyncStorage.getItem('@todo:state').then((state) => {
this.setState(JSON.parse(state))
})
}
state = {
todos: [
{
title: "일기쓰기",
done: true,
},
{
title: "스터디 준비하기",
done: false,
},
],
showModal: false,
}
save = () => {
AsyncStorage.setItem('@todo:state', JSON.stringify(this.state))
}
render() {
return (
<SafeAreaView style={styles.container}>
<Header
show={() => {
this.setState({ showModal: true})
}}
/>
<FlatList
data={this.state.todos}
renderItem={({ item, index }) => {
return (
<TodoItem
title={item.title}
done={item.done}
keyExtractor={(id, index) => {
return id + '${index}'
}}
remove={() => {
this.setState({
todos: this.state.todos.filter((_, i) => i !== index)
}, this.save)
}}
toggle={() => {
const newTodos = [...this.state.todos]
newTodos[index].done = !newTodos[index].done
this.setState({todos: newTodos}, this.save)
}}
/>
)
}}
/>
<TaskModal
isVisible={this.state.showModal}
add={(title) => {
this.setState({
todos: this.state.todos.concat({
title: title,
done: false,
}),
showModal: false,
}, this.save)
}}
hide={() => {
this.setState({ showModal : false })
}}
/>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});