【发布时间】:2019-12-03 19:37:45
【问题描述】:
在 React 的 componentDidMount 中,我从 Firestore 中调用一些数据,其中关联 ID 与字符串匹配。硬编码,一切正常,我们使用 this.props.addStore 将数据添加到 Redux 存储
firebaseApp.firestore().collection('stores').where("associatedID", "==", "LFQ3eJZdbCUrziyoKTV1fVapa2E3").get().then((snapshot)=>{
snapshot.docs.forEach(doc => {
let store = doc.data();
//Tell redux
this.props.addStore(store);
});
}).then(()=>{
console.log(this.props.user.associatedID);
});
但是,如果我们将关联 ID 作为从 Redux 中提取的变量,那么我们会返回一个错误,即“where 的第三个参数未定义”
firebaseApp.firestore().collection('stores').where("associatedID", "==", this.props.user.associatedID)
但在原始代码中,您会注意到在最终的 .then 函数中有一个 this.props.user.associatedID 的控制台日志,它工作得很好。
这向我表明,Redux 应用状态值以供组件使用存在轻微延迟/错误排序/任何情况。或者用通俗的话来说,组件只需要在 componentDidMount 中多一点时间,就可以使用“this.props.user”变量。有没有什么办法可以在没有破解的情况下解决这个问题?
【问题讨论】:
标签: reactjs react-redux google-cloud-firestore