Created
October 15, 2020 05:12
-
-
Save neenjaw/c23d9259b12e8d7ac19f64a6f3cf69e6 to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From Kent C Dodds - https://kentcdodds.com/blog/javascript-to-know-for-react