Skip to content

Instantly share code, notes, and snippets.

@Souvikns
Created July 13, 2021 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Souvikns/aef871d6044de841b8fc420da1a98d96 to your computer and use it in GitHub Desktop.
Save Souvikns/aef871d6044de841b8fc420da1a98d96 to your computer and use it in GitHub Desktop.
import "./styles.css";
import { usePokemon } from "./hook";
export default function App() {
let pokemons = usePokemon();
return (
<div>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<table>
<tr>
<th>ID</th>
<th>Sprite</th>
<th>Name</th>
</tr>
{pokemons.map((el) => (
<tr>
<td>{el.id}</td>
<td>
<img src={el.sprite} alt={el.name} />
</td>
<td>{el.name}</td>
</tr>
))}
</table>
</div>
);
}
import { useState, useEffect } from "react";
import Axios from "axios";
export const usePokemon = () => {
let [pokemons, setPokemons] = useState([]);
const fetchPokemons = async () => {
try {
let { data } = await Axios({
url: "https://klifu.deta.dev/pokemons",
method: "GET"
});
setPokemons(data);
} catch (error) {
console.log(error);
}
};
useEffect(() => {
fetchPokemons();
});
return pokemons;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment