Skip to content

Instantly share code, notes, and snippets.

@TRomesh
Last active May 18, 2021 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TRomesh/9113cb44d54edcd496a4feba2c78db75 to your computer and use it in GitHub Desktop.
Save TRomesh/9113cb44d54edcd496a4feba2c78db75 to your computer and use it in GitHub Desktop.
Handling Asynchronous state
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