Created
September 29, 2019 15:55
-
-
Save robmarshall/4d68f7b3a8d8e70a439f6b0db8e344ec to your computer and use it in GitHub Desktop.
Component using useEffect and Axios to get async data
This file contains 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
import React, { useState, useEffect } from "react"; | |
import axios from "axios"; | |
const fetchDataCall = async ({ api }) => { | |
let apiReturn = await axios | |
.get(api) | |
.then(async function(response) { | |
return response; | |
}) | |
.catch(function(error) { | |
console.log(error); | |
}); | |
return apiReturn; | |
}; | |
const ApiComponent = ({ api }) => { | |
const [data, setData] = useState(""); | |
useEffect(() => { | |
const fetchData = async api => { | |
let response = await fetchDataCall({ api: api }); | |
setData(response); | |
}; | |
fetchData(api); | |
}, []); | |
return ( | |
<div> | |
{data && (make component)} | |
</div> | |
) | |
}; | |
export default ApiComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment