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

132
Vistas
Not able to set state in react properly

I have this function which is setting the user

  const [user, setUser] = useState(null)

  const getUser = async () => {
    setIsAuth(false)
    setLoading(true)
    try {
      let temp = await axiosURL.get('/api/users/me')
      temp = temp.data
      if (temp) {
        setUser(temp)
        setIsAuth(true)
      }
    } catch (error) {
      console.log(error)
    }
    setLoading(false)
  }

After setting the user I am fecthing the data acording to the user

   const getAllScopeData = async () => {
    setLoading(true)
    await getUser()
    console.log(user)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
  }

But when I am running getAllScopeData() The user details is fetching correctly and logging in the console but user from the state is not changing from null . So I am able to fetch further data .

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

0

the problem I see it's here

const getAllScopeData = async () => {
    setLoading(true)
    await getUser()
    console.log(user)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
  }

even if you wait for the function getUser your state is not update yet

You should use useEffect like this

useEffect(async() => {
if(!user) return;
   setLoading(true)
    await getScopeOneData()
    await getScopeTwoData()
    await getScopeThreeData()
    setLoading(false)
}, [user])


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