Created
April 21, 2021 23:26
-
-
Save richleach/e36f0320c7b769f29fae2768b8f4a541 to your computer and use it in GitHub Desktop.
useEffect making async/await axios call with error trapping, and setting to run every time a state var called "stockSymbol" updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
async function getDataAxios() { | |
setLoading(true); | |
await axios({ | |
"method": "GET", | |
"url": "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/get-detail", | |
"headers": { | |
"content-type": "application/octet-stream", | |
"x-rapidapi-host": "apidojo-yahoo-finance-v1.p.rapidapi.com", | |
"x-rapidapi-key": "98dec2a8fbmshfbd792fba05ee2bp12ca09jsn6fa94bdfa33c" | |
}, "params": { | |
"region": "US", | |
"lang": "en", | |
"symbol": stockSymbol | |
} | |
}) | |
.then((response) => { | |
console.log(response.data) | |
setAverageDailyVolume10Day(response.data.summaryDetail.averageDailyVolume10Day.fmt); | |
setMajorHoldersBreakdown(response.data.majorHoldersBreakdown.insidersPercentHeld.fmt); | |
console.log(response.data.insiderHolders.holders); | |
{ response.data.price.regularMarketChange.fmt < 0 ? setQuoteColor('red') : setQuoteColor('green') }; | |
setLoading(false); | |
}) | |
.catch((error) => { | |
console.log(error); | |
setLoading(false); | |
setAxiosError(error); | |
}) | |
} | |
getDataAxios(); | |
}, [stockSymbol]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment