Skip to content

Commit

Permalink
➕ feat: customHook
Browse files Browse the repository at this point in the history
  • Loading branch information
Linder Hassinger committed Apr 26, 2024
1 parent e857520 commit 637d85f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
31 changes: 11 additions & 20 deletions pokedex/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { useState } from "react";
import logo from "./assets/logo.svg";
import pokeSearch from "./assets/poke-search.svg";
import useGetPokemon from "./hooks/useGetPokemon";

function App() {
const [inputValue, setInputValue] = useState("");

const [pokemon, setPokemon] = useState(null);
const result = useGetPokemon(inputValue);

const handleInputValue = (event) => {
setInputValue(event.target.value);
};

const fetchPokemon = async () => {
const url = `https://pokeapi.co/api/v2/pokemon/${inputValue}`;
const response = await fetch(url);

if (!response.ok) {
alert("Hubo un error");
return;
}

const data = await response.json();

setPokemon(data);
};

return (
<>
<main className="p-6">
Expand All @@ -44,7 +31,7 @@ function App() {
</div>
<div>
<button
onClick={fetchPokemon}
onClick={result.fetchPokemon}
className="bg-white px-3 py-2 rounded-lg text-primary font-semibold"
>
Buscar
Expand All @@ -56,19 +43,23 @@ function App() {
<div className="flex items-center justify-center gap-5">
<img
width={150}
src={pokemon?.sprites.other["official-artwork"].front_default}
src={
result.pokemon?.sprites.other["official-artwork"]
.front_default
}
alt=""
/>
<div>
<ul>
<li>Grass</li>
<li>Poison</li>
{result.pokemon?.types.map((type) => (
<li key={type.slot}>{type.type.name}</li>
))}
</ul>
</div>
</div>
<div className="mt-3 bg-gray-200 flex justify-center items-center h-[60px] rounded-b-lg">
<h2 className="text-center font-semibold text-2xl">
{pokemon?.name}
{result.pokemon?.name}
</h2>
</div>
</div>
Expand Down
23 changes: 23 additions & 0 deletions pokedex/src/hooks/useGetPokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState } from "react";

export default function useGetPokemon(inputValue) {
const [pokemon, setPokemon] = useState(null);

const fetchPokemon = async () => {
const url = `https://pokeapi.co/api/v2/pokemon/${inputValue}`;
const response = await fetch(url);

if (!response.ok) {
alert("Hubo un error");
return;
}

const data = await response.json();
setPokemon(data);
};

return {
pokemon,
fetchPokemon,
};
}

0 comments on commit 637d85f

Please sign in to comment.