Skip to content

Instantly share code, notes, and snippets.

@robmarshall
Created September 29, 2019 15:55
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmarshall/4d68f7b3a8d8e70a439f6b0db8e344ec to your computer and use it in GitHub Desktop.
Save robmarshall/4d68f7b3a8d8e70a439f6b0db8e344ec to your computer and use it in GitHub Desktop.
Component using useEffect and Axios to get async data
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