如何分离onSnapshot Listener?< Cloud firestore> [英] How to detach onSnapshot Listener? <Cloud firestore>
本文介绍了如何分离onSnapshot Listener?< Cloud firestore>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用react native开发了我的应用程序,并将Cloud Firestore用于数据库.
我想在用户注销之前分离快照侦听器.
如何在不是 getDataComponent
而是 signOutComponent
中执行 unSubscribe();
?
I've developed my app with react native and used Cloud Firestore for database.
I'd like to detach snapshot listener before users sign out.
How to execute unSubscribe();
in the not getDataComponent
but signOutComponent
?
我的代码在这里:
// getDataComponent.js
class DataComponent extends React.Component {
getData(year) {
const { currentUser } = firebase.auth();
const db = firebase.firestore();
const docRef = db.collection(`users/${currentUser.uid}/statics`).doc(`${year}`);
const unSubscribe = docRef.onSnapshot((doc) => {
// ...
}, (err) => {
// ...
});
}
render() {
return (
// ...
);
}
}
// signOutComponent.js
class signOut extends React.Component {
signOut() {
// I'd like to detach snapshot listner here.
firebase.auth().signOut()
.then(() => {
console.log('signOut success');
})
.catch((error) => {
// ...
});
}
render() {
return (
// ...
);
}
}
我的环境:
"expo": "^34.0.0",
"react": "16.8.3",
"react-native": "https://github.com/expo/react-native/archive/sdk-34.0.2.tar.gz",
"firebase": "^7.14.1",
推荐答案
onSnapshot函数返回取消订阅函数.你可以做
onSnapshot function returns the unsubscribe function. you can do
this.unsubscribe = ref.onSnapshot(...)
,然后在您的componentWillUnmount方法中添加
and in your componentWillUnmount method add
if(this.unsubscribe) {
this.unsubscribe();
}
这篇关于如何分离onSnapshot Listener?< Cloud firestore>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文