Skip to content
Snippets Groups Projects
Commit c0b9600a authored by isaacwsolomon's avatar isaacwsolomon
Browse files

Todos are now dispkayed in a list on the screen

parent 721f5bb5
No related branches found
No related tags found
No related merge requests found
import { addDoc, collection } from 'firebase/firestore'; import { addDoc, collection, onSnapshot } from 'firebase/firestore';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { View, Text, Button, TextInput, StyleSheet } from 'react-native'; import { View, Text, Button, TextInput, StyleSheet } from 'react-native';
import { FIRESTORE_DB } from '../../firebaseConfig'; import { FIRESTORE_DB } from '../../firebaseConfig';
...@@ -7,7 +7,27 @@ import { FIRESTORE_DB } from '../../firebaseConfig'; ...@@ -7,7 +7,27 @@ import { FIRESTORE_DB } from '../../firebaseConfig';
const List = ({navigation} : any) => { const List = ({navigation} : any) => {
const[todos, setTodos] = useState<any[]>([]); const[todos, setTodos] = useState<any[]>([]);
const[todo, setTodo] = useState(''); const[todo, setTodo] = useState('');
useEffect(() => {}, []); useEffect(() => {
const todoRef = collection(FIRESTORE_DB, 'todos');
const subscribe = onSnapshot(todoRef, {
next: (snapshot) => {
console.log('UPDATED');
const todos: any[]= [];
snapshot.docs.forEach((doc) => {
todos.push({
id: doc.id,
...doc.data()
})
})
setTodos(todos);
},
})
return () => subscribe();
}, []);
const addTodo = async () => { const addTodo = async () => {
console.log('ADD'); console.log('ADD');
...@@ -25,6 +45,11 @@ return ( ...@@ -25,6 +45,11 @@ return (
value={todo}/> value={todo}/>
<Button onPress={addTodo} title="Add Todo"disabled={todo === ''}/> <Button onPress={addTodo} title="Add Todo"disabled={todo === ''}/>
</View> </View>
<View>
{todos.map((todo) => (
<Text key={todo.id}>{todo.title}</Text>
))}
</View>
</View> </View>
); );
} }
...@@ -43,7 +68,7 @@ const styles = StyleSheet.create ({ ...@@ -43,7 +68,7 @@ const styles = StyleSheet.create ({
flex: 1, flex: 1,
height: 40, height: 40,
borderWidth: 1, borderWidth: 1,
borderRadius: 1, borderRadius: 4,
padding: 10, padding: 10,
backgroundColor: '#fff', backgroundColor: '#fff',
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment