-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
60 lines (52 loc) · 1.37 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
import React, { useEffect, useState } from 'react';
import { FlatList } from 'react-native';
import firestore from '@react-native-firebase/firestore';
import { Appbar, TextInput, Button, ProgressBar } from 'react-native-paper';
import Todo from './Todo';
const ref = firestore().collection('todos');
const App = () => {
const [todo, setTodo] = useState('');
const [loading, setLoading] = useState(true);
const [todos, setTodos] = useState([]);
async function addTodo() {
await ref.add({
title: todo,
complete: false,
});
setTodo('');
}
useEffect(() => {
return ref.onSnapshot((querySnapshot) => {
const list = [];
querySnapshot?.forEach((doc) => {
const { title, complete } = doc.data();
list.push({
id: doc.id,
title,
complete,
});
});
setTodos(list);
setLoading(false);
});
}, []);
if (loading) {
return <ProgressBar />;
}
return (
<>
<Appbar>
<Appbar.Content title={'TODOs List'} />
</Appbar>
<FlatList
style={{ flex: 1 }}
data={todos}
keyExtractor={(item) => item.id}
renderItem={({ item }) => <Todo {...item} />}
/>
<TextInput label={'New Todo'} value={todo} onChangeText={setTodo} />
<Button onPress={() => addTodo()}>Add TODO</Button>
</>
);
};
export default App;