I have some issues with Firebase in React Native.
I\'m tried to read the data from the Realtime database. I get the data from database and set them into the state an
I think your database listener should look more like this:
const providerRef = firebase.database().ref('providers');
providerRef.on('value', snapshot => {
var newState = [];
console.log(snapshot.val());
snapshot.forEach((childSnapshot) => {
newState.push({
id: childSnapshot.val().id,
username: childSnapshot.val().username,
coordinates: {
longitude: childSnapshot.val().coordinates.longitude,
latitude: childSnapshot.val().coordinates.latitude,
}
});
});
this.setState({ providers: newState })
});
Changes from yours:
newState
inside the callback, so that you start out with an empty array each time you get data from Firebase.setState(...)
only after processing all data from the database, instead of after each item.