Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
Unable to set state after getting document data from Firestore

I am trying to do a User Profile page and after getting the data from Firestore, I'm unable to set the data into the userProfile state.

  const {userProfile, setUserProfile} = useState(null);

  const getUserProfile = async () => {
    await firestore().collection("Users")
    .doc(user.uid)
    .get()
    .then(documentSnapshot => {
      if (documentSnapshot.exists) {
        console.log('User data from Firestore: ', documentSnapshot.data());
        setUserProfile(documentSnapshot.data());
      }
    });    
  }

  useEffect (() => {
    if (userProfile == null) {
      getUserProfile();
      console.log("User Data in userProfile state: ", userProfile);
    }
  }, [userProfile]) 

I got this error:

enter image description here

The console log shows that the userProfile state is undefined, however, I can retrieve the data from Firestore without any issues as shown in the console log.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const {userProfile, setUserProfile} = useState(null); <----- error is here

  const getUserProfile = async () => {
    await firestore().collection("Users")
    .doc(user.uid)
    .get()
    .then(documentSnapshot => {
      if (documentSnapshot.exists) {
        console.log('User data from Firestore: ', documentSnapshot.data());
        setUserProfile(documentSnapshot.data());
      }
    });    
  }

  useEffect (() => {
    if (userProfile == null) {
      getUserProfile();
      console.log("User Data in userProfile state: ", userProfile);
    }
  }, [userProfile]) 

Instead of of const {userProfile, setUserProfile} = useState(null);

replace it with const [userProfile, setUserProfile] = useState(null);

Because u are using curly brackets in {userProfile,setUserProfile}

u need to replace it with square brackets [userProfile,setUserProfile]

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda