Created
April 24, 2025 17:21
-
-
Save DiegoPinho/e1e625166e1980bf1ad12ab9bbc22bde to your computer and use it in GitHub Desktop.
rick-and-morty-api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react' | |
import './App.css' | |
import axios from 'axios'; | |
type Character = { | |
image: string; | |
name: string; | |
} | |
type APIResponse = { | |
info: { | |
count: number; | |
pages: number; | |
next: string | null; | |
previous: string | null; | |
}, | |
results: Character[] | |
} | |
function App() { | |
const [characters, setCharacters] = useState<Character[]>([]); | |
useEffect(() => { | |
async function fetchData() { | |
// TODO: colocar o loading | |
// TODO: tirar a url hardcoded | |
// TODO: fazer um tratamento de erro | |
const result = await axios.get("https://rickandmortyapi.com/api/character/"); | |
const data: APIResponse = result.data; | |
setCharacters(data.results) | |
} | |
fetchData(); | |
}, []) | |
return ( | |
<> | |
{ | |
characters.map(character => { | |
return ( | |
<div> | |
<img src={character.image} /> | |
<p>{character.name}</p> | |
</div> | |
) | |
}) | |
} | |
</> | |
) | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment