Using these two pieces of state:
this.markerArray = [];
this.arrayLength = 0;
I am trying to use this function:
getMarkers = () => {
this.dbRef.get().then((snapshot) => {
snapshot.docs.forEach((marker) => {
if (!this.markerArray.includes(marker.id)) {
let currentID = marker.id;
let appObj = {
...marker.data(),
["id"]: currentID,
};
this.markerArray.push(appObj);
}
});
this.setState({
markerArray: this.markerArray,
});
// (problem code) this.setState({arrayLength: this.markerArray.length})
return this.markerArray;
});
};
To update a list of map markers. I am trying to update this.arrayLength when this.markerArray's length changes. I think the problem has to do with setState's asynchronous nature, but I can't figure out what to do with that information.
I have tried:
I have even tried setting arrayLength to an arbitrary number that does not rely on markerArray.length, but it won't update.