Skip to content

Instantly share code, notes, and snippets.

@khola
Last active March 7, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khola/52874a17ba34d342841dd0b42c9730bf to your computer and use it in GitHub Desktop.
Save khola/52874a17ba34d342841dd0b42c9730bf to your computer and use it in GitHub Desktop.
import { useState, useEffect } from "react";
import { API } from "aws-amplify";
function useAPI(api, endpoint, settings, method = "get") {
const [data, setData] = useState();
const [loading, setLoading] = useState(true);
async function callAPI() {
const response = await API[method](api, endpoint, settings);
setData(response);
setLoading(false);
}
useEffect(() => {
callAPI();
}, []);
return [data, loading];
}
export { useAPI };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment