1 hour ago · Tech · hide

If you're using Firebase Auth in a React app, you probably have a hook like this: import { useState, useEffect } from "react"; import { getAuth, onAuthStateChanged } from "firebase/auth"; const auth = getAuth(); export function useUser() { const [user, setUser] = useState(auth.currentUser); useEffect(() => onAuthStateChanged(auth, setUser), []); return user; } However, that hook has a big problem: it will nearly always return null immediately, resulting in a flash of unauthenticated UI before it fully initializes, even if the user is actually signed in. Why does that happen? Firebase Auth stores login data in IndexedDB, which is asynchronous. Therefore, auth.currentUser will be set to null until the IndexedDB read completes. You could go down the rabbit hole of managing loading states, or integrating with a query library that triggers Suspense until Auth is ready. However, if you're using React Router, there is already an easier way. clientLoader is a special function that your page…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town