Maison > Questions et réponses > le corps du texte
P粉9787424052023-08-19 14:46:23
La raison pour laquelle votre boucle est en cours d'exécution est parce que vous n'avez pas défini onAuthStateChanged() dans l'effet secondaire, veuillez essayer ce qui suit
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;