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

363
Visualizações
How can I handle global state in my Next.js app

if you view the GIF below you can see my project and how the auth, userData and notifications pulls in new data every time a page change happens.

enter image description here

I understand why this is happening, as you can see in my _app.tsx useEffect:

useEffect(() => {
    let userDataUnsub;
    let notificationsUnsub;
    const unsub = auth.onIdTokenChanged(async (user) => {
      if (!user && !currentUser) {
        setAuth(null);
        nookies.set(undefined, "token", "", { path: "/" });
      } else {
        const token = await user.getIdToken();
        setAuth(user);
        toast("New AUTH");
        nookies.set(undefined, "token", token, { path: "/" });
        const docRef = doc(db, "users", user.uid);
        userDataUnsub = onSnapshot(docRef, (doc) => {
          toast("New USERDATA");
          setData(doc.data() as UserData);
        });
        const notificationsQuery = query(
          collection(db, "users", user.uid, "notifications"),
          orderBy("date", "desc"),
          limit(1)
        );
        notificationsUnsub = onSnapshot(notificationsQuery, (querySnapshot) => {
          querySnapshot.forEach((doc) => {
            addNotification(doc.data() as Notification);
            toast("New NOTIFICATION");
          });
        });
      }
    });
    return () => {
      unsub();
      try {
        userDataUnsub();
        notificationsUnsub();
      } catch {}
    };
  }, []);

Since i am using Firebase, I want to get live updates on userData and notifications, however using an onSnapshot in my useEffect means it will do this on every page load, which I dont want. I already have the state from before, I dont want to redownload them.

Does anybody have a way of telling Next.js to wait until the client is loaded before rerunning useEffects again? (bit of a noob question I know, but im unsure of how to tackle this)

about 4 years ago · Juan Pablo Isaza
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