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

354
Views
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
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!