Created
April 2, 2020 03:15
-
-
Save jsmanifest/de2af6112da0f48af3eb7740328dbdfa to your computer and use it in GitHub Desktop.
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
import React from 'react' | |
function useStuff() { | |
const [data, setData] = React.useState({}) | |
React.useEffect(() => { | |
fetch('https://someapi.com/api/users/') | |
.then((response) => setData(response.json())) | |
.catch((err) => setData(err)) | |
}, []) | |
return { data, setData } | |
} | |
function App() { | |
const { data } = useStuff() | |
if (data instanceof Error) { | |
return <p style={{ color: 'red' }}>Error: {data.message}</p> | |
} | |
return <div>{JSON.stringify(data, null, 2)}</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment