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

188
Vistas
React - is there a way to set the state in a function and use the updated state right after in another function?

I have two functions, one which creates an User Plan and another which creates an UserGoal. Inside the first function I want to set the UserPlanId in order to use it in the second function. Both these functions are called inside a handleSubmit function.

However, I don't know if it is possible to make that work. I tried many things but none worked for getting the updated state inside the second function.

const handleSubmit = async (event) => {
event.preventDefault();
const resp = await axios
  .post("https://localhost:44325/UserPlans/create", {
    bmi: bmi,
    totalCalories: parseInt(totalCalories),
    totalCarbs: parseInt(carbs),
    totalProtein: parseInt(protein),
    totalFat: parseInt(fat),
  })
  .then((response) => {
    console.log(response.data);
    setUserPlan(response.data);
    setUserPlanId(response.data.id);

  })
  .catch((error) => {
    console.log(error.response.data);
  });

await axios
  .post("https://localhost:44325/UserGoals/create", {
    Goal: goalInt,
    GoalWeight: kilosGoal,
    UserActivity: activityInt,
    UserPlanId: userPlanId, // HERE I WANT THE UPDATED STATE
  });}

Are there any options for doing 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