【发布时间】:2020-07-13 10:25:29
【问题描述】:
我正在从我的 react 本机应用程序中的组件 A 更新 firebase 集合中的一些数据。例如,我正在更新 firebase 集合 X 中的 name 参数。名称的更新是从组件 A 完成的。组件 B 正在从同一个集合 X 中获取名称。但是要查看组件 B 中的更改,我必须重新加载应用程序或再次刷新应用程序。但我想在不刷新或重新加载应用的情况下查看更改。
组件 A
async setName(){
await firestore()
.collection('set_name')
.doc(auth().currentUser.uid)
.update({
name: this.state.name}).then( () => {
console.log('name updated')
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
}
组件 B
componentDidMount(){
this.getName();
}
async getName(){
let name;
await firestore()
.collection('set_name')
.doc(auth().currentUser.uid)
.get().then(function(doc){
this.setState({name:doc.data()}))
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
}
我可以从组件 B 中的 firebase 获取数据,但要查看更改,我必须重新加载组件。但我不想重新加载看到组件 B 的任何变化。
【问题讨论】:
标签: javascript reactjs firebase react-native google-cloud-firestore