Skip to content

Instantly share code, notes, and snippets.

@TylerSustare
Created January 10, 2020 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerSustare/5698e55126cedaeefcecdfcf390db743 to your computer and use it in GitHub Desktop.
Save TylerSustare/5698e55126cedaeefcecdfcf390db743 to your computer and use it in GitHub Desktop.
firebase realtime database data
import React, { Component } from 'react'
import firebase from 'firebase';
export default class List extends Component {
constructor(props) {
super(props);
this.state = {
loaded: false,
data: {},
};
}
componentDidMount() {
firebase.database().ref('notes/').on('value', snapShot => {
this.setState({ data: snapShot.val(), loaded: true })
});
}
render() {
return (
<div>
{this.state.loaded ? this.renderList() : null}
</div>
)
}
renderList = () => {
const { data } = this.state;
return (
<div>
{Object.keys(data).map((a, i) => {
return (
<div key={i}>
<p key={`data${i}`}>{JSON.stringify(data[a])}</p>
</div>
)
})}
</div>
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment