-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
executable file
·65 lines (61 loc) · 1.7 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
import React, { useState } from "react";
import {
FlatList,
Alert,
TouchableWithoutFeedback,
Keyboard,
} from "react-native";
import Header from "./components/Header";
import ToDoItem from "./components/ToDoItem";
import AddNewItem from "./components/AddNewItem";
import { Container, Content, List, SafeAreaContainer } from "./styles";
export default function App() {
const [toDos, setToDos] = useState([
{ text: "go to the gym", key: 0 },
{ text: "buy some food", key: 1 },
{ text: "create an app", key: 2 },
{ text: "do code review", key: 3 },
]);
const pressHandler = (key) => {
setToDos((prevTodos) => {
return prevTodos.filter((toDo) => toDo.key != key);
});
};
//New State
const submitHandler = (text) => {
if (text.length > 3) {
setToDos((prevToDos) => {
return [{ text: text, key: Math.random().toString() }, ...prevToDos];
});
} else {
Alert.alert("Oops!", "ToDos Must be over 3 chars long", [
{ text: "OK!", onPress: () => console.log("Alert closed") },
]);
}
};
return (
<SafeAreaContainer>
<TouchableWithoutFeedback
onPress={() => {
Keyboard.dismiss();
console.log("dismissed keyboard");
}}
>
<Container>
<Header />
<Content>
<AddNewItem submitHandler={submitHandler} />
<List>
<FlatList
data={toDos}
renderItem={({ item }) => (
<ToDoItem item={item} pressHandler={pressHandler} />
)}
/>
</List>
</Content>
</Container>
</TouchableWithoutFeedback>
</SafeAreaContainer>
);
}