如何在状态下设置监听器?
How to set listener in the state?
我有一个 Firestore onSnapshot 侦听器侦听文档的更改,但如果我将它设置为如下状态,我似乎无法理解它是如何工作的:
constructor(props) {
super(props);
this.fetchChatsListener = null;
}
我是这样设置的:
this.fetchChatsListener = firestore().collection('chats').doc(documentSnapshot.id).collection('messages').where('created', '>=', currenttime);
this.state.sent_message_id != '' ? this.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : ''
this.fetchChatsListener.orderBy('created', 'desc')
.onSnapshot({
error: (e) => console.error(e),
next: (querySnapshot_) => {
querySnapshot_.docChanges().forEach(change => {
this.state.dataSource.push({
...change.doc.data(),
key: change.doc.id,
});
});
this.setState({
chatEntered: true,
dataSource: this.state.dataSource,
refreshingMessages: false,
current_chat_id: documentSnapshot.id
});
},
});
我调用这个函数取消订阅:
unsubscribe_chats_listener() {
this.fetchChatsListener;
this.fetchChatsListener = null;
}
但不幸的是它没有取消订阅,因为我不断收到新的更改。
更新
当我尝试退订时使用:
this.fetchChatsListener();
失败并出现此错误:
TypeError: this.fetchChatsListener is not a function
OP代码只提到退订功能,需要调用
unsubscribe_chats_listener() {
this.fetchChatsListener(); // note the parens, causing invocation
this.fetchChatsListener = null;
}
编辑
this.fetchChatsListener
正在初始化为查询。查询不是取消订阅功能。
this.fetchChatsListener = firestore().collection('chats') // a collection ref
.doc(documentSnapshot.id) // a doc ref
.collection('messages') // a collection ref
.where('created', '>=', currenttime); // a query
onSnapshot()
returns取消订阅功能,所以设置属性那里...
let query = firestore().collection('chats')
.doc(documentSnapshot.id)
.collection('messages')
.where('created', '>=', currenttime);
this.state.sent_message_id != '' ? query.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : ''
query.orderBy('created', 'desc')
this.fetchChatsListener = query.onSnapshot({ // an unsubscribe function
error: (e) => console.error(e),
next: (querySnapshot_) => {
// ...
我有一个 Firestore onSnapshot 侦听器侦听文档的更改,但如果我将它设置为如下状态,我似乎无法理解它是如何工作的:
constructor(props) {
super(props);
this.fetchChatsListener = null;
}
我是这样设置的:
this.fetchChatsListener = firestore().collection('chats').doc(documentSnapshot.id).collection('messages').where('created', '>=', currenttime);
this.state.sent_message_id != '' ? this.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : ''
this.fetchChatsListener.orderBy('created', 'desc')
.onSnapshot({
error: (e) => console.error(e),
next: (querySnapshot_) => {
querySnapshot_.docChanges().forEach(change => {
this.state.dataSource.push({
...change.doc.data(),
key: change.doc.id,
});
});
this.setState({
chatEntered: true,
dataSource: this.state.dataSource,
refreshingMessages: false,
current_chat_id: documentSnapshot.id
});
},
});
我调用这个函数取消订阅:
unsubscribe_chats_listener() {
this.fetchChatsListener;
this.fetchChatsListener = null;
}
但不幸的是它没有取消订阅,因为我不断收到新的更改。
更新
当我尝试退订时使用:
this.fetchChatsListener();
失败并出现此错误:
TypeError: this.fetchChatsListener is not a function
OP代码只提到退订功能,需要调用
unsubscribe_chats_listener() {
this.fetchChatsListener(); // note the parens, causing invocation
this.fetchChatsListener = null;
}
编辑
this.fetchChatsListener
正在初始化为查询。查询不是取消订阅功能。
this.fetchChatsListener = firestore().collection('chats') // a collection ref
.doc(documentSnapshot.id) // a doc ref
.collection('messages') // a collection ref
.where('created', '>=', currenttime); // a query
onSnapshot()
returns取消订阅功能,所以设置属性那里...
let query = firestore().collection('chats')
.doc(documentSnapshot.id)
.collection('messages')
.where('created', '>=', currenttime);
this.state.sent_message_id != '' ? query.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : ''
query.orderBy('created', 'desc')
this.fetchChatsListener = query.onSnapshot({ // an unsubscribe function
error: (e) => console.error(e),
next: (querySnapshot_) => {
// ...