can you fetch data if the user is logged in in the react js context api?

In you useEffect you can add auth check and call firestore.

useEffect(() => {
    let firestoreCall;
    if(auth) {
    firestoreCall  = firebase
      .firestore()
      .collection("richieste")
      /*  .where("userId", "==", props.loggedIn.uid) */

      .onSnapshot((snapshot) => {
        const newRichieste = snapshot.docs.map((richiesta) => ({
          id: richiesta.id,
          ...richiesta.data(),
        }));
        let nuoveRichieste = newRichieste.sort(function (a, b) {
          return a.timestamp - b.timestamp;
        });

        setRichieste(nuoveRichieste.reverse());
        setInCaricamento(false);
      });  
    }
    

    return () => {
        firestoreCall?.();
    };
  }, [auth]);


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top