Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda