Last active
July 20, 2018 20:32
-
-
Save kaiobrito/f13924a0ac382c4ca6f9686b76b6eca0 to your computer and use it in GitHub Desktop.
Todo list + Firebase + React Component
This file contains 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 from "react"; | |
import firebase from "firebase"; | |
import config from "./config.json"; | |
firebase.initializeApp(config); | |
class TodoList extends React.Component { | |
state = { | |
todos: [] | |
}; | |
componentDidMount() { | |
// #1 | |
const todoRef = firebase.database().ref("todos"); | |
// #2 | |
this.listener = todoRef.on("value", snapshot => { | |
// #3 | |
this.setState({ todos: snapshot.val() || [] }); | |
}); | |
} | |
componentWillUnmount() { | |
// #4 | |
this.listener.off(); | |
} | |
render() { | |
return ( | |
<ul> | |
{this.state.todos.map(todo => <li key={todo.id}>{todo.text}</li>)} | |
</ul> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment