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

155
Vistas
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
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