So the problem I'm having is that the setState method doesn't seem to want to update the state of my array questions. But when I use console.log(returnArr) the console prints my desired item from firebase.
What I'm I doing wrong?
export default class App extends React.Component {
constructor() {
super();
this.state = {
questions: [],
current_question: 0
};
}
componentDidMount() {
var rootRef = firebase.database().ref();
var ref = rootRef.child("Geography");
var returnArr = [];
ref
.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var item = childSnapshot.val();
item.key = childSnapshot.key;
if (item.key === "2") {
returnArr.push(item.Question);
setState = () => ({
questions: returnArr
});
console.log(returnArr);
}
});
})
.catch(error => {
console.log(error);
});
}
render() {
// ...
}
}