diff --git a/pokedex/src/App.jsx b/pokedex/src/App.jsx index 7d00f93..8c4298e 100644 --- a/pokedex/src/App.jsx +++ b/pokedex/src/App.jsx @@ -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 ( <>
@@ -44,7 +31,7 @@ function App() {
diff --git a/pokedex/src/hooks/useGetPokemon.js b/pokedex/src/hooks/useGetPokemon.js new file mode 100644 index 0000000..ca6af45 --- /dev/null +++ b/pokedex/src/hooks/useGetPokemon.js @@ -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, + }; +}