Last active
May 18, 2021 04:07
-
-
Save TRomesh/9113cb44d54edcd496a4feba2c78db75 to your computer and use it in GitHub Desktop.
Handling Asynchronous state
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
const fetchResource = () => fetch('https://pokemons.com').then(r => r.json()) | |
const state = useState(fetchResource); | |
if (state.promised) { | |
return <p>Loading {resourcePath}</p>; | |
} | |
if (state.error) { | |
return (<p> | |
Failed to load {resourcePath} | |
<br /> | |
<h3 style={{ color: 'red' }}>{state.error.toString()}</h3> | |
<br /> | |
<button onClick={() => state.set(fetchResource)}>Retry</button> | |
</p>) | |
} | |
return (<p> | |
Loaded {resourcePath}<br /> | |
<h3 style={{ color: 'green' }}>{state.name}</h3> | |
<h4 style={{ color: 'blue' }}>{state.power}</h4> | |
<h5 style={{ color: 'yellow' }}>{state.description}</h5> | |
</p>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment