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

181
Views
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
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!