Created
April 25, 2020 16:55
-
-
Save suhas86/44a026831836a5ba21be9cc57f6ace58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState, useEffect} from "react"; | |
import Todo from "./Todo"; | |
import Divider from "@material-ui/core/Divider"; | |
import {todosRef} from "./firebase"; | |
function TodoList() { | |
const [todos,setTodos] = useState<any>([]); | |
useEffect(() => { | |
todosRef.on('value', (snapshot) => { | |
let items = snapshot.val(); | |
let newState = []; | |
for (let item in items) { | |
newState.push({ | |
id: item, | |
task: items[item].task, | |
done: items[item].done | |
}); | |
} | |
setTodos(newState) | |
}); | |
},[]) | |
return ( | |
<> | |
{todos.map((todo: any, i: number) => ( | |
<React.Fragment key={todo.id}> | |
<Todo todo={todo} /> | |
{i<todos.length -1 && <Divider />} | |
</React.Fragment> | |
))} | |
</> | |
); | |
} | |
export default TodoList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment