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

157
Vistas
How to convert react useAsync hook to await?

I have a component that calls an api asynchronously and renders other things until the promise is resolved. But now I want the response of that call inorder to render conditionally, but doing this I keep running into errors like 'Rendered more hooks than during the previous render'. Can anyone suggest how can I convert the following into await?

export default function HomePage(props) {
  const {response, err} = useAsync({promiseFn:getUserDetails}) 

const [globalState] = useContext(Context);
const userClass = globalState.isUserStudent;
getStudentService(userClass);

useEffect(()=>{
setSession()
},[])

if(err){
return(
<>
<div>{state.defaultUser}</div>
</>
)
}

if(response){
const userName = response.Name;

return (
<>
<div>{userName}</div>
</>
)
}
return null
}

Now I want to send 1 more parameter in getStudentService i.e response value from getUserDetails. If I do a promise.Resolve, nothing is rendered on screen and I see if called 3 times. If I move the getStudentService call code inside if(response), I get the Rendered too many times error.

How to resolve this?

about 4 years ago · Juan Pablo Isaza
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