Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!