Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
firebase data not being updated with use effect

hi i have to refresh my page to see the effect of the person adding an event to the calendar:

my code is

const handleDateClick = async (DateClickArg) => {
  if (DateClickArg) {
    const title = prompt("Enter title", DateClickArg.dateStr); // allows user to put a title in

    // making object
    const event = {
      title: title ? title : DateClickArg.dateStr,
      start: DateClickArg.date,
      allDay: true,
    };

    allEvents.push(event);

    const db = fire.firestore();
    let currentUserUID = fire.auth().currentUser.uid;
    const doc = await fire
      .firestore()
      .collection("userCalendar")
      .doc(currentUserUID)
      .get();

    db.collection("userCal/" + currentUserUID + "/activities").add({ event });
  }
};

and my getuserinfo is:

const getUserInfo = async () => {
  let currentUserUID = fire.auth().currentUser.uid;

  const qSnap = await fire
    .firestore()
    .collection("userCal")
    .doc(currentUserUID)
    .collection("activities")
    .get();

  const data = [];
  data = qSnap.docs.map((d) => ({
    id: d.id,
    title: d.data().event.title,
    start: d.data().event.start.toDate(),
    allDay: d.data().event.allDay,
    ...d.data(),
  }));

  //setData(data)
  console.log(data);
  setData([...data]);
};

useEffect(() => {
  let mounted = false;

  if (!mounted) {
    getUserInfo();
  }

  return () => {
    mounted = true;
  };
}, []);

where am i going wrong with my use effect? is there a way for the data to update in the browser once its added to firebase? i am using react full calendar

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using get() only returns a point-in-time snapshot of your data. If you want to listen for realtime updates, use .onSnapshot() instead.

You'll also need to make sure you unsubscribe from updates when your component is cleaned up

useEffect(() => {
  const currentUserUID = fire.auth().currentUser.uid;

  return fire
    .firestore()
    .collection("userCal")
    .doc(currentUserUID)
    .collection("activities")
    .onSnapshot(({ docs }) => {
      setData(
        docs.map((doc) => {
          const data = doc.data();
          return {
            id: doc.id,
            title: data.event.title,
            start: data.event.start.toDate(),
            allDay: data.event.allDay,
            ...data,
          };
        })
      );
    });
}, []);

.onShapshot() returns an unsubscribe function so returning that from your effect hook will run it when your component is unmounted.

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming your firebase call is ok, there is an error inside your useEffect call. You are setting the mounted variable wrong, it is supposed to be false when your component is destroyed and true after your component is rendered. Also, to avoid unexpected behaviors I highly recommend using the useRef hook to check that.

function Component() {
  const isMounted = useRef(false)

  useEffect(() => {
    isMounted.current = true;

    if (isMounted) {
      getUserInfo();
    }

    return () => { isMounted.current = false }
  }, []);

  ...
}

export default Component;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda