Skip to content

Instantly share code, notes, and snippets.

@kaiobrito
Last active July 20, 2018 20:32
Show Gist options
  • Save kaiobrito/f13924a0ac382c4ca6f9686b76b6eca0 to your computer and use it in GitHub Desktop.
Save kaiobrito/f13924a0ac382c4ca6f9686b76b6eca0 to your computer and use it in GitHub Desktop.
Todo list + Firebase + React Component
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