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

151
Views
setState and run function on same event

How can i set the state of a variable using setState and then use same variable inside a function that gets called into a click event?

function example(){
const [state,setState]=useState()

 async function getData(){
  try{
   --some code--
   const example1={field: state};
   };
  catch(){}
}
 return <button 
  onClick{()=>{
   setState();
   getData();
 }}>
  </button>
}

As you can see i am trying to use setState and also a function that contains the state variable inside the same onClick event. Problem is, i have to click twice for it to work. Basically first click does setState and second click runs the function. How can i execute both setState and getData inside the same onClick event?

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!