Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Is there a way to update a returned value from a JS function?

I am making a chat app using Firebase and RN. In my firebase code I have a function like this:

//all values are declared before, db is from firebase config which i do not wish to share
import "firebase";
async function getPublic(dba = db) {
  const messages = onSnapshot(doc( /*collection name ->*/"public", dba), db => db.docs())
  return messages;
}

Is there a way to update the returned value or something similar to that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of naming your function getPublic, consider instead usePublic. Or even better, generalize it so you can use different paths.

But first, we need to look at the definition of onSnapshot() (a CollectionReference extends from Query):

export declare function onSnapshot<T>(query: Query<T>, observer: {
    next?: (snapshot: QuerySnapshot<T>) => void;
    error?: (error: FirestoreError) => void;
    complete?: () => void;
}): Unsubscribe;

As you can see here, the messages aren't returned from this function, but an Unsubscribe function is (a () => void). So to update a messages array, you'll need to use useState and because you are using realtime listeners, you should use useEffect to manage the listener lifecycle. You also should handle the intermediate states such as loading, errored and fetched data. This results in:

import { useEffect, useState } from 'react';
import { getFirestore, collection, onSnapshot } from "firebase/firestore";

function useMessageFeed(feed = "public", firestore = getFirestore()) { // use default firestore instance unless told otherwise
  // set up somewhere to store the data
  const [ messagesInfo, setMessagesInfo ] = useState(/* default messagesInfo: */ {
    status: "loading",
    messages: null,
    error: null
  });
  
  // attach and manage the listener
  useEffect(() => {
    const unsubscribe = onSnapshot( // unsubscribe is a () => void
      collection(/* firestore instance: */ firestore, /* collection path: */ feed),
      {
        next: querySnapshot => setMessagesInfo({
          status: "loaded",
          messages: querySnapshot.docs(), // consider querySnapshot.docs().map(doc => ({ id: doc.id, ...doc.data() }))
          error: null
        }),
        error: err => setMessagesInfo({
          status: "error",
          messages: null,
          error: err
        })
      }
    );

    return unsubscribe;
  }, [firestore, feed]); // <-- if these change, destroy and recreate the listener
  
  return messagesInfo; // return the data to the caller
}

Elsewhere in your code, you would use it like this:

const SomeComponent = (props) => {
  const { status, messages, error: messagesError } = useMessageFeed("public");
  
  switch (status) {
    case "loading":
      return null; // hides component
    case "error":
      return (
        <div class="error">
          Failed to retrieve data: {messagesError.message}
        </div>
      );
  }

  // render messages
  return (
    /* ... */
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!