-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApp.js
115 lines (106 loc) · 2.59 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
import React, { useState, useEffect } from "react";
import {
StyleSheet,
Text,
TextInput,
View,
Button,
ScrollView,
} from "react-native";
import AppBar from "./src/components/AppBar";
import AddTodo from "./src/components/AddTodo";
import TodoItem from "./src/components/TodoItem";
export default function App() {
const [title, setTitle] = useState("");
// Initalize empty array to store todos
const [todos, setTodos] = useState([]);
// function to add todo object in todo list
const addTodo = () => {
if (title.length > 0) {
// Add todo to the list
setTodos([...todos, { key: Date.now(), name: title, isChecked: false }]);
// clear the value of the textfield
setTitle("");
}
};
// function to mark todo as checked or unchecked
const checkTodo = (id) => {
// loop through todo list and look for the the todo that matches the given id param
// update the state using setTodos function
setTodos(
todos.map((todo) => {
if (todo.key === id) {
todo.isChecked = !todo.isChecked;
}
return todo;
})
);
};
// function to delete todo from the todo list
const deleteTodo = (id) => {
// loop through todo list and return todos that don't match the id
// update the state using setTodos function
setTodos(
todos.filter((todo) => {
return todo.key !== id;
})
);
};
useEffect(() => {
console.log(todos.length, "TodoList length");
//console.log(todos);
}, [todos]);
return (
<View style={styles.container}>
<View style={styles.statusBar}></View>
<AppBar />
<View style={styles.todo}>
<AddTodo
placeholder="Add a todo"
title={title}
setTitle={setTitle}
style={styles.textbox}
addTodo={addTodo}
/>
</View>
<ScrollView>
{todos.map((todo) => (
<TodoItem
key={todo.key}
todo={todo}
checkTodo={checkTodo}
deleteTodo={deleteTodo}
/>
))}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
statusBar: {
backgroundColor: "#7F39FB",
color: "#fff",
width: "100%",
height: 30,
},
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "flex-start",
},
todo: {
flexDirection: "row",
width: "100%",
justifyContent: "center",
alignItems: "center",
},
textbox: {
borderWidth: 1,
borderColor: "#7F39FB",
borderRadius: 8,
padding: 10,
margin: 10,
width: "80%",
},
});