(
);
}
export default App;
// lib/fetch-all.js
export default async (urls, options) => {
const responses = await Promise.all(
urls.map((url) => fetch(
url,
options,
)),
);
return Promise.all(
responses.map((response) => response.json()),
);
};
// PokemonList.js
import React, { useState, useEffect } from 'react';
import fetchAll from './lib/fetch-all';
import Pokemon from './Pokemon';
function PokemonList({ pokemons }) {
const [pokemonsData, setPokemonsData] = useState([]);
useEffect(() => {
const abortController = new AbortController();
const { signal } = abortController;
(async () => {
let jsonResults;
try {
jsonResults = await fetchAll(
pokemons.map((name) => `https://pokeapi.co/api/v2/pokemon/${name}/`),
{ signal },
);
} catch (err) {
return setPokemonsData([]);
}
return setPokemonsData(jsonResults.map((data) => ({
name: data.name,
weight: data.weight,
img: data.sprites.front_default,
})));
})();
return () => abortController.abort();
}, [pokemons]);
return pokemonsData.map(({ name, weight, img }) => (
));
}
export default PokemonList;