ReactFix
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • React
  • React Native
  • Programming
  • Object Oriented Programming

Thursday, November 17, 2022

[FIXED] How do I detect if axios request is in loading phase while using async/await?

 November 17, 2022     async-await, axios, javascript, reactjs   

Issue

I need to detect if the axios request (with async/await) is in loading phase. Because, I need to disable a button if the request is loading.

So, this is the line of code:

const response = await API.get("/url", options);

Now, I need to disable a certain button if this request is in loading phase. But, I'm stuck on how to solve this issue.


Solution

setState(true) //to initate loading
const response = await API.get("/url", options);
setState(false) //to turn off loading
try{
setState(true) //to initate loading
const response = await API.get("/url", options);
}
catch(err){}
finally{
setState(false) //to turn off loading
}


Answered By - Mohammed Shahed
Answer Checked By - - Pedro (ReactFix Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

Featured Post

Is Learning React Difficult?

React is difficult to learn, no ifs and buts. React isn't inherently difficult to learn. It's a framework that's different from ...

Total Pageviews

Copyright © ReactFix