Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
How to set listener in the state?

I have a Firestore onSnapshot listener listening to changes on a document but I cant seem to understand how it works if I set it in the state like below:

constructor(props) {
 super(props);         
 this.fetchChatsListener = null;
} 

and this is how I set it:

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
        });

      },
    });

I call this function to unsubscribe from it:

unsubscribe_chats_listener() {
  this.fetchChatsListener;
  this.fetchChatsListener = null;
}

but unfortunately it does not unsubscribe because I keep receiving new changes.

UPDATE

when I try to unsubscribe using:

this.fetchChatsListener();

it fails with this error:

TypeError: this.fetchChatsListener is not a function
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The OP code only mentions the unsubscribe function, it needs to invoke it.

unsubscribe_chats_listener() {
  this.fetchChatsListener();  // note the parens, causing invocation
  this.fetchChatsListener = null;
}

EDIT

this.fetchChatsListener is being initialized to a query. Query's aren't unsubscribe functions.

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 the unsubscribe function, so set the property there...

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_) => {
      // ...
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda