-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
37 lines (34 loc) · 1.07 KB
/
example.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
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { setPokemon, setError } from "../../actions";
import getPokemons from "../../api/getPokemons";
import PokemonList from "../../components/PokemonList";
import Searcher from "../../components/Searcher";
import "./styles.css";
function Home() {
const dispatch = useDispatch();
const pkmns = useSelector((state) => state.list);
useEffect(() => {
const fetchData = async () => {
try {
const res = await getPokemons();
const { results: pkmns } = await res.json();
const result = await Promise.all(pkmns.map((pkmn) => fetch(pkmn.url)));
const pokemonsData = await Promise.all(
result.map((pkmn) => pkmn.json())
);
dispatch(setPokemon(pokemonsData));
} catch (err) {
dispatch(setError({ message: "Ocurrió un error", err }));
}
};
fetchData();
}, []);
return (
<div className='Home'>
<Searcher />
<PokemonList pokemons={pkmns} />
</div>
);
}
export default Home;