Skip to content

Instantly share code, notes, and snippets.

@juandc
Forked from kentcdodds/fetch-pokemon.js
Created October 8, 2018 15:19
Show Gist options
  • Save juandc/571da377d91e780567516ba3e5e74ed3 to your computer and use it in GitHub Desktop.
Save juandc/571da377d91e780567516ba3e5e74ed3 to your computer and use it in GitHub Desktop.
// NOTE: This is NOT how you'd normally use suspense.
// You'll either use react-cache or libraries that use react-cache.
// This is only for instructional purposes.
const cache = {}
function FetchPokemon({pokemonName}) {
const pokemon = cache[pokemonName]
if (!pokemon) {
const promise = fetchPokemon(pokemonName).then(
pokemon => (cache[pokemonName] = pokemon),
)
throw promise
}
return <pre>{JSON.stringify(pokemon || 'Unknown', null, 2)}</pre>
}
function PokemonInfo({pokemonName}) {
return (
<React.Placeholder fallback="loading...">
<FetchPokemon pokemonName={pokemonName} />
</React.Placeholder>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment