This content originally appeared on DEV Community and was authored by Haruka Kakiuchi
Using Pokemon api
https://pokeapi.co/
How to get all data
import { useEffect } from 'react';
import { getAllPokemon } from './utils/pokemon'
function App() {
const initialURL = `https://pokeapi.co/api/v2/pokemon/`;
useEffect(() => {
const fetchPokemonData = async function() {
let res = await getAllPokemon(initialURL)
console.log(res);
}
fetchPokemonData();
}, [])
return (
<div className="App">
</div>
);
}
export const getAllPokemon = function (url) {
return new Promise((resolve, reject) => {
fetch(url)
.then((res) => res.json())
.then((data) => resolve(data));
});
};
Read each of data
const [pokemonData, setPokemonData] = useState([])
useEffect(() => {
const fetchPokemonData = async function() {
let res = await getAllPokemon(initialURL)
loadPokemon(res.results) // added
setLoading(false)
}
fetchPokemonData();
}, [])
const loadPokemon = async function(data) {
let _pokemonData = await Promise.all(data.map((item) => {
let pokemonRecord = getPokemon(item.url)
return pokemonRecord;
}))
setPokemonData(_pokemonData)
}
This content originally appeared on DEV Community and was authored by Haruka Kakiuchi
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Haruka Kakiuchi | Sciencx (2024-06-30T20:41:18+00:00) How to fetch API – React. Retrieved from https://www.scien.cx/2024/06/30/how-to-fetch-api-react/
" » How to fetch API – React." Haruka Kakiuchi | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/how-to-fetch-api-react/
HARVARDHaruka Kakiuchi | Sciencx Sunday June 30, 2024 » How to fetch API – React., viewed ,<https://www.scien.cx/2024/06/30/how-to-fetch-api-react/>
VANCOUVERHaruka Kakiuchi | Sciencx - » How to fetch API – React. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/how-to-fetch-api-react/
CHICAGO" » How to fetch API – React." Haruka Kakiuchi | Sciencx - Accessed . https://www.scien.cx/2024/06/30/how-to-fetch-api-react/
IEEE" » How to fetch API – React." Haruka Kakiuchi | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/how-to-fetch-api-react/. [Accessed: ]
rf:citation » How to fetch API – React | Haruka Kakiuchi | Sciencx | https://www.scien.cx/2024/06/30/how-to-fetch-api-react/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.