Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created October 15, 2020 05:12
Show Gist options
  • Save neenjaw/c23d9259b12e8d7ac19f64a6f3cf69e6 to your computer and use it in GitHub Desktop.
Save neenjaw/c23d9259b12e8d7ac19f64a6f3cf69e6 to your computer and use it in GitHub Desktop.
function GetGreetingForSubject({subject}) {
const [isLoading, setIsLoading] = React.useState(false)
const [error, setError] = React.useState(null)
const [greeting, setGreeting] = React.useState(null)
React.useEffect(() => {
async function fetchGreeting() {
try {
const response = await window.fetch('https://example.com/api/greeting')
const data = await response.json()
setGreeting(data.greeting)
} catch (error) {
setError(error)
} finally {
setIsLoading(false)
}
}
setIsLoading(true)
fetchGreeting()
}, [])
return isLoading ? (
'loading...'
) : error ? (
'ERROR!'
) : greeting ? (
<div>
{greeting} {subject}
</div>
) : null
}
@neenjaw
Copy link
Author

neenjaw commented Oct 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment