【发布时间】:2016-09-10 23:41:17
【问题描述】:
在我的 react Native 应用程序中单击一个按钮时,我会调用两个函数:getSolutionListFromDatabase,它将组件的状态设置为包含此解决方案列表,然后是 updateDatabaseSolutionList,它将一个元素添加到此列表并推送它回到 Firebase。然而,虽然应用程序的状态在第一个函数中被正确更新,但在第二个函数中,状态被记录为未定义,并且我的该函数的日志语句在第一个函数中的某些语句之前被调用。函数是否出于某种原因异步运行,这是 React native 的特性吗?如果是这样,在设置状态之前如何防止第二个函数执行?谢谢。
onSubmitPressed: function() {
if (this.checkSolution) {
this.getSolutionListFromDatabase();
this.updateDatabaseSolutionList();
Alert.alert(
'Correct!',
"Woohoo!"
);
}
},
getSolutionListFromDatabase: function() {
var thisItemRef = itemsListRef.child(this.state.itemID);
thisItemRef.once('value', (snap) => {
var solutionList = snap.val();
this.setState({
solutionList: solutionList
});
console.log('solution is set as' + this.state.solutionList);
});
},
updateDatabaseSolutionList: function() {
var newSolutionList = [];
console.log('solutionList undefined here' + this.state.solutionList);
if (this.state.solutionList) {
newSolutionList = this.state.solutionList;
newSolutionList.push(this.props.itemID);
}
//then push new list to Firebase
},
【问题讨论】:
标签: ios reactjs firebase react-native firebase-realtime-database