Skip to content

Instantly share code, notes, and snippets.

@AlexCharlton93
Created February 21, 2021 17:43
Show Gist options
  • Save AlexCharlton93/131226aacb84d59b299d28cf6cd32e2a to your computer and use it in GitHub Desktop.
Save AlexCharlton93/131226aacb84d59b299d28cf6cd32e2a to your computer and use it in GitHub Desktop.
API Call
const MyComponent = ({ fetchData }) => {
const [data, setData] = useState(null);
const [dataError, setDataError] = useState(null);
useEffect(() => {
fetchData()
.then(setData)
.catch(setDataError);
}, []);
if (dataError) {
return <div>{dataError}</div>
}
return <div>{data}</div>;
};
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment