I am working on a expense splitting app and have saved user details in firebase database. Now I am trying to fetch the user details from Firebase DB in ComponentDidMount method and rendering it to screen.
The problem is that the user details are not appearing immediately. It’s only when I do something in the app, the user details appears in the screen. I know it is the async behavior that is stopping the details to appear right away.
But there has to be a way to get the details as soon as the promise is resolved inside ComponentDidMount method.
How do I go about it?
Here is the code snippet:
this.state = {
friendsList:[]
};
componentDidMount() {
let friendsList = [];
firebase.database().ref("friendsList")
.once('value')
.then((snapshot) => {
snapshot.forEach((childSnapshot) => {
friendsList.push({
...childSnapshot.val(),
id:childSnapshot.key
})
})
})
this.setState({friendsList})
}
const {friendsList} = this.state
// snippet from the react render call below
<div className={classes.friendsDashboard}>
{friendsList.map(friend => {
return (
<div>
{friend.name}
</div>
);
})}
</div>
Any help/hint/suggestion would be greatly appreciated.
Happy New Year!