-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pokemon.js
70 lines (52 loc) · 2.23 KB
/
Pokemon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// useEffect: HTTP requests
// http://localhost:3000/isolated/exercise/06.js
import React, {useEffect,useState} from 'react'
// 🐨 you'll want the following additional things from '../pokemon':
// fetchPokemon: the function we call to get the pokemon info
// PokemonInfoFallback: the thing we show while we're loading the pokemon info
// PokemonDataView: the stuff we use to display the pokemon info
import {PokemonForm,PokemonInfoFallback,PokemonDataView } from '../pokemon'
function PokemonInfo({pokemonName}) {
[pokemon,setPokemon]=useState()
useEffect(()=>{fetchPokemon(pokemonName).then(
pokemonData => {setPokemon(pokemonData)},
)},[pokemonName])
// 🐨 Have state for the pokemon (null)
// 🐨 use React.useEffect where the callback should be called whenever the
// pokemon name changes.
// 💰 DON'T FORGET THE DEPENDENCIES ARRAY!
// 💰 if the pokemonName is falsy (an empty string) then don't bother making the request (exit early).
// 🐨 before calling `fetchPokemon`, clear the current pokemon state by setting it to null.
// (This is to enable the loading state when switching between different pokemon.)
// 💰 Use the `fetchPokemon` function to fetch a pokemon by its name:
// fetchPokemon('Pikachu').then(
// pokemonData => {update all the state here },
// )
// 🐨 return the following things based on the `pokemon` state and `pokemonName` prop:
// 1. no pokemonName: 'Submit a pokemon'
// 2. pokemonName but no pokemon: <PokemonInfoFallback name={pokemonName} />
// 3. pokemon: <PokemonDataView pokemon={pokemon} />
// 💣 remove this
const pokemon=null;
if (!pokemonName)
return 'Submit a pokemon'
else if (!pokemon)
return <PokemonInfoFallback name={pokemonName} />
else return <PokemonDataView pokemon={pokemon} />
}
function App() {
const [pokemonName, setPokemonName] = useState('')
function handleSubmit(newPokemonName) {
setPokemonName(newPokemonName)
}
return (
<div className="pokemon-info-app">
<PokemonForm pokemonName={pokemonName} onSubmit={handleSubmit} />
<hr />
<div className="pokemon-info">
<PokemonInfo pokemonName={pokemonName} />
</div>
</div>
)
}
export default App