-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
117 lines (117 loc) · 2.55 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
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
import { StatusBar } from "expo-status-bar";
import {
StyleSheet,
Text,
TextInput,
View,
Button,
SafeAreaView,
} from "react-native";
import React, { useState, useEffect } from "react";
import AsyncStorage from '@react-native-async-storage/async-storage';
export default function App() {
const [task, onChangeText] = useState("");
const [tasks, setTasks] = useState([]);
useEffect(() => {
AsyncStorage.getItem("tasks").then((data) => {
if (data) {
setTasks(JSON.parse(data));
}
});
setTasks([...tasks]);
}, []);
const storeData = async (value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem("tasks", jsonValue);
} catch (e) {
console.log(e);
}
};
useEffect(() => {
storeData(tasks);
}, [tasks]);
const addTask = (text) => {
if (text === "") {
return;
}
if (tasks.includes(text)) {
return;
}
onChangeText("");
setTasks([...tasks, text]);
};
const removeTask = (index) => {
let newTasks = [...tasks];
newTasks.splice(index, 1);
setTasks(newTasks);
};
return (
<SafeAreaView>
<View style={styles.container}>
<TextInput
style={styles.inp}
placeholder="enter a task:"
value={task}
onChangeText={onChangeText}
/>
<Button
title="Add Task"
style={styles.mainbtn}
onPress={() => addTask(task)}
/>
</View>
<View style={styles.elements}>
<Text style={styles.head}>Tasks:</Text>{tasks.map((task, index) => (
<View style={styles.task}>
<Text key={index}>{task}</Text>
<Button
title="done"
onPress={() => removeTask(index)}
/>
</View>
))}
</View>
<StatusBar style="auto" />
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "row",
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
margin: 27,
},
elements: {
display: "flex",
flexDirection: "column",
alignItems: "center",
backgroundColor: "#fff",
},
head: {
fontSize: 20,
fontWeight: "bold",
color: "black",
},
inp: {
borderWidth: 1,
borderColor: "black",
padding: 10,
margin: 10,
width: "70%",
},
task: {
flexDirection: "row",
justifyContent: "space-between",
width: "80%",
backgroundColor: "#f0f0f0",
margin: 5,
},
mainbtn: {
width: "30%",
padding: 10,
},
});