P粉9787424052023-08-19 14:46:23
The reason your loop is running is because you did not set onAuthStateChanged() in the side effect, please try the following
const FirebaseAuth = React.createContext({ user: null, loading: true, role: '', email: '', firstName: '', isActive: false, lastName: '', location: '', userID: '', }); export const FirebaseAuthProvider = ({ children }) => { const [user, setUser] = useState(null); const [role, setRole] = useState(null); const [email, setEmail] = useState(''); const [firstName, setFirstName] = useState(''); const [isActive, setIsActive] = useState(false); const [lastName, setLastName] = useState(''); const [location, setLocation] = useState(''); const [userID, setUserID] = useState(''); const [loading, setLoading] = useState(true); useEffect(() => { onAuthStateChanged(auth, (user) => { setLoading(true); if (user) { setUser(user); FirebaseFirestoreService.getUserData(user.uid).then((data) => { setUserID(user.uid); setUserRole(data.role); setEmail(data.email); setFirstName(data.firstName); setIsActive(data.isActive); setLastName(data.lastName); setLocation(data.location); }); setLoadingState(false); } else { setLoadingState(false); } }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return ( <FirebaseAuth.Provider value={{ user, loading, role, email, firstName, lastName, isActive, location, userID, }} > {children} </FirebaseAuth.Provider> ); }; export default FirebaseAuth;