ã¯ããã«
æ¬èšäºã§ã¯ãPokeAPIãå©çšããŠãã±ã¢ã³ã®ååãéžæãããšå¯Ÿå¿ããç»åã衚瀺ããç°¡åãªã¢ããªãäœæããŸãããããéããŠãAPIã䜿ã£ãããŒã¿ååŸã®çµéšãç©ã¿ãReactãçšããAPIéä¿¡ã®åºæ¬æäœã«æ £ããããšãç®æããŸãã
ä»åã¯ã³ãŒãã®å ·äœçãªèª¬æã¯ãããAPIã䜿ãã€ã¡ãŒãžãæŽãã§ãããããšã«éç¹ã眮ããŠããŸããéäžã§äœãããŠããã®ãããããªããªã£ãŠããŸã£ãå Žåã§ããæçµç³»ã®ã³ãŒããèŒããŠããŸãã®ã§åäœã®ç¢ºèªãŸã§å®æœããã¢ããªãåãåã³ãäœæããŸãããïŒ
çè ã¯ReactãåŠã³å§ããŠ1幎ã®åå¿è ã§ãããã®ããããã®èšäºã¯Reactåå¿è åãã®å 容ãšãªã£ãŠããŸããäžç·ã«åŠã³ãªããåãçµãã§ãããŸãããïŒ
äºåç¥è
æ¬èšäºã§ã¯ReactãAPIã«ã€ããŠããçšåºŠã®äºåç¥èãå¿
èŠãšããŸãã
APIã«ã€ããŠã¯åèèšäºãèŒããŠãããŸãã®ã§ãå¿
èŠã«å¿ããŠäžèªãã ããã
éçºç°å¢ã®æºå
ä»åãéçºãã¹ã ãŒãºã«å§ãããããããã³ãã¬ãŒãçšæããŸããã
ã¿ãŒããã«ãããä»»æã®ãã£ã¬ã¯ããªã§äžèšã®ãªããžããªãcloneããŠãã ããã
ã³ãã³ãã¯ä»¥äžã§ãã
git clone https://github.com/ReactLearningDepot/poke-api-practice.git
cloneãå®äºããããwebãã£ã¬ã¯ããªã«ç§»åãããããžã§ã¯ãã§å¿ èŠãšãªãããã±ãŒãžãã€ã³ã¹ããŒã«ããŸãã
cd poke-api-practice/web
npm install
以äžã§éçºæºåã¯å®äºã§ãã
PokeAPIã䜿ã£ããã±ã¢ã³ããŒã¿ååŸ
ãªãŒãã³ãœãŒã¹ã«ããååŸ
ããã§ã¯ãæ¬é¡ã«å
¥ããŸãã
ã³ãŒããæžãå§ããåã«ãAPIã䜿ã£ãŠããŒã¿ãååŸããã€ã¡ãŒãžãæŽããããPokeAPIãæäŸããç¡æã®ãªãŒãã³ãœãŒã¹ã§ãã±ã¢ã³ããŒã¿ãååŸããŠã¿ãŸãããã
以äžã®ãµã€ãã«ã¢ã¯ã»ã¹ããŠãã ããã
ä»åã¯æåã«ãã±ã¢ã³ããŒã¿ã®äžèŠ§ãååŸããŸãã
ãã©ãŒã ã«pokemonãšå
¥åããsubmitãæŒããŸãã
ç»åã®éããã¬ã¹ãã³ã¹ãšããŠçµæãè¿ã£ãŠãããšæããŸããããã§resultsã¿ãã®çŽäžã«ãã±ã¢ã³ããŒã¿ã®äžèŠ§ãæ ŒçŽãããŠããããšãèŠããŠãããŠãã ããããã±ã¢ã³ããŒã¿ã«ã¯ååãšURLãèšèŒãããŠããŸãã
ããã§ã¯äžèŠ§ã®äžåç®ã«è¡šç€ºãããŠãããã±ã¢ã³ã®URLãããäžåºŠãã©ãŒã ã«æã£ãŠã¿ãŸãããã
ãã©ãŒã ã«ã¯ãã¹ã®ã¿å
¥åãæ±ããããŠãããããpokemon/1ãšå
¥åããŸãã
ãããšãå
ã»ã©ãšã¯ç°ãªãçµæãè¿ã£ãŠããŸããã
ã©ããªæ
å ±ãè¿ã£ãŠããŠããããããã«ããã§ããããã¹ã«æ°åãæå®ããããšã§ãããã±ã¢ã³ã«é¢ãããªãããã®æ
å ±ãè¿ã£ãŠããŠããããšãæ³åã§ããŸãã
ãã©ãŒã ã«æå®ãããã¹(ãšã³ããã€ã³ã)ãšã¬ã¹ãã³ã¹ã®å¯Ÿå¿ã«ã€ããŠã¯æ¬èšäºã®äºåç¥èã«æ²èŒããPokeAPIã®ããã¥ã¡ã³ãã«èšèŒããããŸãã
ã¢ããªã±ãŒã·ã§ã³ã«ããååŸ
ãããŸã§ã§PokeAPIã䜿ã£ãŠãã±ã¢ã³ããŒã¿ãååŸããã€ã¡ãŒãžãã€ãããšæããŸãã
ããããã¯åæ§ã®æäœãå®éã«ã¢ããªã±ãŒã·ã§ã³äœæããŠè¡ã£ãŠãããŸãã
App.tsxã®å®è£ ïŒãã±ã¢ã³ã®ååãååŸïŒ
App.tsxãéããŠãã ããã
ã¯ããã«ãuseStateãèšè¿°ããŸãã
useStateã¯ãã±ã¢ã³ããŒã¿ã®ç¶æ
ãä¿æãããã®å€æŽã«å¿ããŠç»é¢ãæŽæ°ããä»çµã¿ãæäŸããŸããããã§ãååŸããããŒã¿ã®åãšããŠGetPokemonTypeãäœæããuseStateã«ã¯é
åã§åãæå®ããŸãã
GetPokemonTypeã®äžèº«ã¯ãåè¿°ã®ãã±ã¢ã³ããŒã¿ã«å«ãŸããååãšURLãå®çŸ©ããŸãã
export default function App() {
const [pokemons, setPokemons] = useState<GetPokemonType[]>([])
return (
<>
</>
)
}
type GetPokemonType = {
name: string,
url: string
}
次ã«ãuseEffect
ãçšããŠããŒãžãã¬ã³ããªã³ã°ããéã«å®è¡ããåŠçãèšè¿°ããŠãããŸãã
Reactã§APIéä¿¡ãè¡ãæ段ã®1ã€ãšããŠfetch
ã䜿ã£ãæ¹æ³ããããŸãã®ã§ä»åã¯fetch
ã䜿çšããŸãã
以äžã®ã³ãŒããèšè¿°ããŠãã ããã
fetch
ã®åŒæ°ã«ã¯ãªãŒãã³ãœãŒã¹ã§å
¥åããURLãæå®ããŸãã
ãã®åŸã«ç¶ãthen
ã¯éåæåŠçãšåŒã°ãããã®ã§ãããããã§ã¯èª¬æãçç¥ããŸããããããããªãå Žåã¯å床Reactã®åºç€åŠç¿ã«åãçµãã§ã¿ãŸãããã
setPokemonsãå®è¡ããããšã§ååŸãããã±ã¢ã³ããŒã¿ãä¿æããŸãã
ããã§ã»ããããŠããresults
ãPokeAPIã®ãªãŒãã³ãœãŒã¹ã§ã確èªããããŒã§ãã
useEffect(() => {
fetch('https://pokeapi.co/api/v2/pokemon')
.then((res) => res.json())
.then((data) => setPokemons(data.results))
}, []);
次ã«ãååŸããããŒã¿ã衚瀺ããviewãå®è£
ããŠãããŸãã
ãã±ã¢ã³ããŒã¿ãä¿æããŠããpokemons
ã¯é
åã®ãããé«éé¢æ°map
ãçšããŠãã±ã¢ã³ã®ååããªã¹ãã§è¡šç€ºãããŸãã
return
ã«ä»¥äžã®ã³ãŒããèšè¿°ããŠãã ããã
return (
<>
<ul>
{ pokemons.map((pokemon) => (
<li key={pokemon.name}>
{pokemon.name}
</li>
))}
</ul>
</>
)
ãããŸã§ã§ãã±ã¢ã³ã®ååããªã¹ãã§è¡šç€ºãããããšãã§ããŠããã¯ãã§ãã
äžèšã³ãã³ãã§ãµãŒããŒãèµ·åãããã©ãŠã¶ã§ç¢ºèªããŠã¿ãŸãããã
npm run dev
ç»åã®ããã«ãã±ã¢ã³ã®ååããªã¹ãã§è¡šç€ºãããŠããããšãããããŸãã
ååŸãããããŒã¿æ°ã¯ããã©ã«ãã§20åã§ãã
ããŒã¿æ°ãæå®ããŠååŸããå Žåã¯ä»¥äžã®ããã«ã¯ãšãªãã©ã¡ãŒã¿ã«limitãæå®ããŸã
https://pokeapi.co/api/v2/pokemon?limit=100
åèã«ããããŸã§ã®ã³ãŒãã¯ä»¥äžã®ããã«ãªã£ãŠããŸãã
import {useEffect, useState} from "react";
import './App.css'
export default function App() {
const [pokemons, setPokemons] = useState<GetPokemonType[]>([])
useEffect(() => {
fetch('https://pokeapi.co/api/v2/pokemon')
.then((res) => res.json())
.then((data) => setPokemons(data.results))
}, []);
return (
<>
<ul>
{ pokemons.map((pokemon) => (
<li key={pokemon.name}>
{pokemon.name}
</li>
))
}
</ul>
</>
)
}
type GetPokemonType = {
name: string,
url: string
}
App.tsxã®å®è£ ïŒéžæãããã±ã¢ã³ã®ç»åãååŸïŒ
ç¶ããŠããã±ã¢ã³ã®ååãéžæããéã«ã察象ã®ç»åãååŸããŠè¡šç€ºããŠãããŸãã
ããã§ã¯ãååãéžæããæã«ç»åãååŸããåŠçãè¡ãé¢æ°ãšãååŸããç»åæ
å ±ã管çããä»çµã¿ãuseState
ã§å®è£
ããŸãããããã®éãuseState
ã«æå®ããåãšããŠGetPokemonSpritesType
ãå®çŸ©ããŸãã
é¢æ°handleSelectPokemon
ã¯ãç»åååŸã«å¿
èŠãªãã¹ïŒãšã³ããã€ã³ãïŒãæã€URLãåŒæ°ãšããŠåãåãããã®URLãçšããŠåã³fetch
ãè¡ããŸãã
const [pokemonSprites, setPokemonSprites] = useState<GetPokemonSpritesType | null>(null)
const handleSelectPokemon = (url: string) => {
fetch(url)
.then((res) => res.json())
.then((data) => setPokemonSprites(data['sprites']['other']['official-artwork']))
}
type GetPokemonSpritesType = {
front_default: string,
}
ç»åã®ååŸãã§ããã®ã§ç»åã衚瀺ããviewãå®è£
ããŠãããŸãã
return
ã®äžã§li
ã¿ã°ãã¯ãªãã¯ããéã«å
ã»ã©ã®é¢æ°ãåŒã³åºãåŠçãšããŠonClick
ãèšè¿°ããŠãã ããã
ãŸããç»åã衚瀺ããimg
ã¿ã°ãèšè¿°ããŸãã
ããã§ãclassName
ãè¿œå ã«ãªã£ãŠããŸãããä»åã¯äºåã«App.css
ã§ç°¡åãªã¹ã¿ã€ã«ãæå®ããŠãããclassNameãèšè¿°ããã ãã§CSSãé©çšãããããã«ãªã£ãŠããŸãïŒimportãå¿
èŠã§ãïŒã
return (
ã<div className='container'>
<ul>
{pokemons.map((pokemon) => (
<li key={pokemon.name} onClick={() => handleSelectPokemon(pokemon.url)}>
{pokemon.name}
</li>
))
}
</ul>
<div className='pokemon-image'>
{pokemonSprites && <img src={pokemonSprites?.front_default} alt="pokemon-image"/>}
</div>
</div>
)
以äžã§å®è£
ã¯å®äºã§ãã
åèã«ããããŸã§ã®ã³ãŒãã¯ä»¥äžã®ããã«ãªã£ãŠããŸãã
import {useEffect, useState} from "react";
import './App.css'
export default function App() {
const [pokemons, setPokemons] = useState<GetPokemonType[]>([])
const [pokemonSprites, setPokemonSprites] = useState<GetPokemonSpritesType | null>(null)
const handleSelectPokemon = (url: string) => {
fetch(url)
.then((res) => res.json())
.then((data) => setPokemonSprites(data['sprites']['other']['official-artwork']))
}
useEffect(() => {
fetch('https://pokeapi.co/api/v2/pokemon?limit=20')
.then((res) => res.json())
.then((data) => setPokemons(data.results))
}, []);
return (
<div className='container'>
<ul>
{pokemons.map((pokemon) => (
<li key={pokemon.name} onClick={() => handleSelectPokemon(pokemon.url)}>
{pokemon.name}
</li>
))
}
</ul>
<div className='pokemon-image'>
{pokemonSprites && <img src={pokemonSprites?.front_default} alt="pokemon-image"/>}
</div>
</div>
)
}
type GetPokemonType = {
name: string,
url: string
}
type GetPokemonSpritesType = {
front_default: string,
}
ããã§ã¯æåŸã«ã¢ããªã±ãŒã·ã§ã³ã觊ã£ãŠã¿ãŸãããã
äžèšã³ãã³ãã§ãµãŒããŒãèµ·åãããã©ãŠã¶ã§ç¢ºèªããŠã¿ãŸãããã
npm run dev
ç»åã®ããã«ãã±ã¢ã³ã®ååãéžæãããšãã±ã¢ã³ã®ç»åã衚瀺ããããšæããŸãã
ãç²ãæ§ã§ããïŒ
ãŸãšã
æ¬èšäºã§ã¯PokeAPIãå©çšããŠç°¡åãªã¢ããªãäœæããããšã§ãAPIã䜿ã£ãããŒã¿ååŸãå®è·µããŠã¿ãŸããã
ãããŸã§è¿·ããé²ããããŸããã§ãããããä»åã¯ã³ãŒãã®å
·äœçãªèª¬æã¯çç¥ããAPIã䜿ãã€ã¡ãŒãžãæŽãã§ãããããšã«éç¹ã眮ãããããèªåã§äžããæžãããäžå®ã«æããæ¹ãããã£ãããããšæããŸãã
çè
ãã¯ããã³ãŒããèŠãæã¯äœãèµ·ãã£ãŠããã®ããã£ã±ããããããäžå®ãæããŠããŸãããããŸãã¯å®å
šã«ç解ãããããã®ã§ã¯ãªããã³ãŒããæžãããåãããšããçµéšããã®åã³ãå¢ãããŠããããšã倧äºã ãšä»ã¯æã£ãŠããŸãã
ãŸããä»åã¯åããŠèšäºã®æçš¿ã«ãã£ã¬ã³ãžããŠã¿ãŸããã
æ®æ®µä»äºãªã©ã§åŠãã æè¡ãè³ç£ãšããŠæ®ããŠãããããšæã£ãŠã®ããšã§ãã
ä»åŸã¯æ¥åã§äœ¿çšããŠããReactãSwiftUIãSpringBootãAWSãªã©ã«ã€ããŠãåŠãã ããšãã¢ãŠããããããŠãããããšæããŸããäžæ
£ããèšäºã«çŸããããšãããããšæããŸããããããããèŠå®ã£ãŠããã ãããšå¹žãã§ãã